Determine if the string is palindrome
Question:
This function takes a string as input and returns True if string is palindrome and
False otherwise. A palindrome is a symmetric sequence of characters, reading
the same forward and backward.
For example: radar, anna, mom, dad, …
Answers:
You can do this like the below:
def is_palindrome(phrase):
string = ""
for char in phrase:
if char.isalnum():
string += char
print(string)
return string[::-1].casefold() == string.casefold()
word = input("Please enter a word to check: ")
if is_palindrome(word):
print("'{}' is a palindrome".format(word))
else:
print("'{}' is not a palindrome".format(word))
This function takes a string as input and returns True if string is palindrome and
False otherwise. A palindrome is a symmetric sequence of characters, reading
the same forward and backward.
For example: radar, anna, mom, dad, …
You can do this like the below:
def is_palindrome(phrase):
string = ""
for char in phrase:
if char.isalnum():
string += char
print(string)
return string[::-1].casefold() == string.casefold()
word = input("Please enter a word to check: ")
if is_palindrome(word):
print("'{}' is a palindrome".format(word))
else:
print("'{}' is not a palindrome".format(word))