Computer Science
Write a Python function that checks whether a passed string is a palindrome or not.
Note: A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run.
Python
Python Functions
1 Like
Answer
def is_palindrome(s):
s = s.lower()
return s == s[::-1]
input_string = input("Enter a string: ")
if is_palindrome(input_string):
print(input_string, "is a palindrome.")
else:
print(input_string, "is not a palindrome.")
Output
Enter a string: madam
madam is a palindrome.
Enter a string: nursesrun
nursesrun is a palindrome.
Enter a string: watermelon
watermelon is not a palindrome.
Answered By
1 Like
Related Questions
Write a Python function to calculate the factorial of a number (a non-negative integer). The function accepts the number whose factorial is to be calculated as the argument.
Write a Python function that takes a number as a parameter and checks whether the number is prime or not.
Write a Python program that accepts a hyphen-separated sequence of words as input and prints the words in a hyphen-separated sequence after sorting them alphabetically.
Sample Items: green-red-yellow-black-white
Expected Result: black-green-red-white-yellowWhat is a recursive function? Write one advantage of recursive function.