A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward.
def is_palindrome(s):
return s == s[::-1]
# Taking input from the user
input_string = input("Enter a string to check if it's a palindrome: ")
if is_palindrome(input_string):
print("Yes, it's a palindrome")
else:
print("No, it's not a palindrome")
Enter a string to check if it's a palindrome: racecar
Yes, it's a palindrome
The function is_palindrome(s)
takes a string s
as input and returns True
if the string is a palindrome (reads the same forwards and backwards), and False
otherwise.
The function checks if the input string is equal to its reverse using Python's slicing feature s[::-1]
.
The example usage section demonstrates how to take input from the user, check if it's a palindrome, and print the result.