In: Computer Science
Write a complete Python program that asks the user for a line of text (not just a single word), and then tells the user whether the string is a palindrome (same forward and backward) if you ignore case and non-alphabetic characters. The following methods and functions may be useful: str.upper(), str.isalpha() -> bool, reversed(str) -> sequence, str.find(str) -> int, str.replace(str, str), str.center(int). Unless otherwise specified, they all return a string.
Program :
def Palindrome(s): #find given text is palindrome or not
l, h = 0, len(s) - 1
s = s.upper() # Change text to Upper case letters
while (l <= h):
if (not(s[l] >= 'A' and s[l] <= 'Z')):
l += 1
elif (not(s[h] >= 'A' and s[h] <= 'Z')):
h -= 1
elif (s[l] == s[h]):
l += 1
h -= 1
else:
return False
return True
text =str(input("Enter a sentence:")) #read text from user
if Palindrome(text)==True:
print("Text is palindrome!!");
else:
print("Text is not a palindrome!!")
Output :
Thank you Have a great day!!! Please do like.