
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.
Have Questions? Post them here!
- Program 11: Calculate Percentage - 1000+ Python Programs
- Program 8: Multiply Two Numbers - 1000+ Python Programs
- Program 1: Print Hello World! - 1000+ Python Programs
- 20 - Python - Print Colors for Text in Terminal - 1000+ Python Programs
- Program 15: Find String is a Palindrome or Not - 1000+ Python Programs
- 17: Find Factorial of a Number - 1000+ Python Programs
- Program 13: Reverse a String - 1000+ Python Programs
- 18: Get Sub List By Slicing a Python List - 1000+ Python Programs
- Program 9: Divide Two Numbers - 1000+ Python Programs
- Program 7: Find Difference of Two Numbers - 1000+ Python Programs
- Program 14: Sum of Even Numbers from 1 to 100 - 1000+ Python Programs
- Program 10: Modulo of Two Numbers - 1000+ Python Programs
- 16: Find the largest element in a List - 1000+ Python Programs
- Python Program: Use NumPy to generate a random number between 0 and 1
- Program 2: Print your name using print() function - 1000+ Python Programs
- 19: Simple Calculator using built-in functions - 1000+ Python Programs
- Program 6: Find Sum of Two Floating Numbers - 1000+ Python Programs
- Program 12: Calculate Area and Circumference of Circle - 1000+ Python Programs
- Program 5: Find Sum of Two Integer Numbers - 1000+ Python Programs
- Program 4: Input Name and Age and Print - 1000+ Python Programs
- Program 3: Print the name of user as input - 1000+ Python Programs
- MySQL Query for Schema/Structure of a Table - MySQL
- SCP Copy all files from directory to Local Folder - Linux
- Fix: Microsoft OneDrive We are currently experiencing technical difficulties - Microsoft
- How to Rename a Git Local Branch Using the --move Option - Git
- Docker Commit Command with Examples - Docker
- Move from Zsh to Bash shell macOS - MacOS
- How to run Java Unit Test cases with Apache Maven? - Java
- [Solved] Dynamic Web Module 3.0 requires Java 1.6 or newer Mac OSX - Mac-OS-X