CBSE Class 12 Computer Science Question 63 of 101

Functions — Question 31

Back to all questions
31
Question

Question 27

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.

Solution
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.
Answer