
What is a palindrome?
A palindrome is a string that reads the same forwards and backward.
Example: Hannah, Ava, Ada, Elle, Eve, Anna, Otto, Bob, and Radar.In this program, we will check if a given string is a palindrome or not.
Pseudo Logic:
1. First, we take the input string from the user.
2. Next we convert the string to lowercase.
3. We then reverse the string and store it in a new variable as reversed_string.
4. We compare the reversed_string with the original_string.
5. If both strings are the same, then we say that the given string is a palindrome.
Otherwise, it is not a palindrome.
Palindrome Python Code
# Python Program no. 15
# Check for Palindrome
# 1000+ Python Programs by Code2care.org
original_string = input("Enter a string: ")
lowercase_string = original_string.lower()
reversed_string = lowercase_string[::-1]
if lowercase_string == reversed_string:
print(f"The given string {lowercase_string } is a palindrome.")
else:
print(f"The given string {lowercase_string} is not a palindrome.")
Let's do some testing
Enter a string: Ada
The given string Ada is a palindrome.
Enter a string: Sammual
The given string Sammual is a not palindrome.
Enter a string: Hannah
The given string Hannah is a palindrome.
Python Methods/Concepts used:
- Input() function - to take input from the user. Python Doc: https://docs.python.org/3/library/functions.html#input
- String slicing - to reverse the string. Python Doc: https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str
Related Alternate Questions:
- Write a Python program to check if a given number is a palindrome or not.
- Write a Python program to find all the palindromic substrings in a given string.
- Write a Python program to find the longest palindrome in a given string.
- Write a Python program to check if a given string is an anagram of a palindrome or not.
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!