Issue
Determine if a sentence is a palindrome (read the same in both directions).
Example:
text = "MrOwl ate my metal worm" -> This is a palindrome
text = "Rose fell"-> This is not a palindrome
text = 'MrOwl ate my metal worm'.lower().split()
for i in text: #this is where the problem arises, I go through all the elements, but I don't know how to put them back together, so that later I can work as with a string
text1=-?
print('MrOwl ate my metal worm" -> This is a palindrome' if text1[::-1] == text1 else "This is not a palindrome")
Solution
First remove the whitespaces and use the slice
notation to invert the string and check if it matches the original:
original = 'MrOwl ate my metal worm'
text = original.replace(" ", "").lower()
if text[::-1] == text:
print(f"{original} is a palindrome")
Answered By - Pedro Maia
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.