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

A string containing a word is given as an input to your Python program. Your task is to check each and every word of the particular string  if the word is palindrome or not.  if the word is palindrome printed output.

 

 

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

 

 



Palindrome string definition
===================================

A given string is said to be a palindrome  if the reverse of the string is same as the original string


let's take an example
===========================

Let's consider string : aibohphobia
If we reverse the  above string  we get a result as aibohphobia  which is same as the above string so aibohphobia   so is a palindrome string





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

Define a function named as find_palindrome_words()  and parse a string variable as an input to this Python function.  Take the empty list variable palindrome and initialize it to an empty list.


Using the for loop, iterate all the words present in the list by splitting it with the space.  use the if statement to check if the word is equal to the reverse of the word it's so appended  the word to the palindrome list.

def find_palindrome_words(data):
   palindromes = []
   for word in data.split(" "):
       if word == word[::-1]:
           palindromes.append(word)
   return palindromes






Let's take a new  string variable and ask a user to enter a string using the input function call. Once the user enters a string, call the above function find_palindrome_words() and pass the user entered string as input to the function.  the function will return list of word containing string,  store the the palindrome list into a new variable named as word


Use a print statement to print the user friendly message and then using the for loop print all the words present in the list of words are palindrome.

text = input("Enter a String : ")
words = find_palindrome_words(text)
print("The palindrome words in the string are ... ")
for word in words:
   print(word)





Complete python working code
==============================

def find_palindrome_words(data):
   palindromes = []
   for word in data.split(" "):
       if word == word[::-1]:
           palindromes.append(word)
   return palindromes

text = input("Enter a String : ")
words = find_palindrome_words(text)
print("The palindrome words in the string are ... ")
for word in words:
   print(word)




Output
================

Enter a String : madam is palindrome word
The palindrome words in the string are ...
madam




OUTPUT
==================

Enter a String : madam and malayalam are two words
The palindrome words in the string are ...
madam
malayalam




OUTPUT
==================

Enter a String : there is no palindrome string in this text
The palindrome words in the string are ...




Conclusion
===============

Execute the above program by providing a string containing palindrome words and note down the result.

Comment it down below if you have any suggestions or queries





Post a Comment

0 Comments