Write a Python Function to Find All The Words in a String Which Are Palindrome

Provided a string as input to the Python program the string containing the palindrome words  and the non palindrome words inside it,  you have to write a Python function  to separate all the words that are palindrome and return it to the main program.

Let's do this using the Python function.

 

 

Write a Python Function to Find All The Words in a String Which Are Palindrome


Write a Python Function to Find All The Words in a String Which Are Palindrome

Let's define a function named find_palindrome()  and pass the user provided input string as an input argument to this function.  take an empty list variable  and use the for loop  and try to split all the words from the given string  using the space.

 Once you get each and every word of the string, try to compare the string with the reverse of the string using the string slicing  method [::-1].  and also within the if statement  check if the length of that particular word is not 1.  If you have  found the palindrome word, append the palindrome word to the list of palindromes.


Finally using the return statement returns the list containing palindrome.

def find_palindrome(data):
    palindrome = []
    for word in data.split(" "):
        if word == word[::-1] and len(word) != 1:
            palindrome.append(word)

    return palindrome




Main program take a variable named as text and using the input function ask the user to enter a string the string may contain palindrome and non palindrome words so  so let's call the function find_palindrome()  by passing the string in lowercase letter by converting it into to lower()

text = input("Enter a String : ")
result = find_palindrome(text.lower())




When the function gets executed try to catch the return value from the function using the new variable result.  The result variable will contain all the words which are palindromes in the given string so using the for loop iterate all the items present in the list variable result  and print all the words.

print("Palindrome words in string are ...")
for word in result:
    print(word)


 

Code
===================
def find_palindrome(data):
    palindrome = []
    for word in data.split(" "):
        if word == word[::-1] and len(word) != 1:
            palindrome.append(word)

    return palindrome


text = input("Enter a String : ")
result = find_palindrome(text)

print("Palindrome words in string are ...")
for word in result:
    print(word)






OUTPUT
=================
Enter a String : malayalam is a language
Palindrome words in string are ...
malayalam




OUTPUT
==============
Enter a String : how are you madam ?
Palindrome words in string are ...
madam




OUTPUT
==============
Enter a String : Madam speaks malayalam and she is suffering from aIbohphobiA
Palindrome words in string are ...
madam
malayalam
aibohphobia




Conclusion
=================
Execute the above  Python program by providing the random string containing palindrome words and non palindrome words as an input to the program.  note down the results.


Comment down below if you have any other  method to solve this Python program.



Post a Comment

0 Comments