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.
Facing issues? Have Questions? Post them here! I am happy to answer!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to Python-Programs,
- Program 5: Find Sum of Two Integer Numbers - 1000+ Python Programs
- 34: Traverse a List in Reverse Order - 1000+ Python Programming
- 22: Send Yahoo! Email using smtplib - SMTP protocol client using Python Program
- 35: Python Program to find the System Hostname
- 27: Measure Elapsed Time for a Python Program Execution
- Program 7: Find Difference of Two Numbers - 1000+ Python Programs
- Program 12: Calculate Area and Circumference of Circle - 1000+ Python Programs
- Program 9: Divide Two Numbers - 1000+ Python Programs
- Program 2: Print your name using print() function - 1000+ Python Programs
- 25: How to rename a file using Python Program
- 17: Find Factorial of a Number - 1000+ Python Programs
- Program 6: Find Sum of Two Floating Numbers - 1000+ Python Programs
- 23: Python Programs to concatenate two Lists
- 36: Python Program Convert Hex String to Integer
- 20 - Python - Print Colors for Text in Terminal - 1000+ Python Programs
- Python Program: Use NumPy to generate a random number between 0 and 1
- 32: Python Program to Find Square Root of a Number
- Program 8: Multiply Two Numbers - 1000+ Python Programs
- Program 11: Calculate Percentage - 1000+ Python Programs
- 18: Get Sub List By Slicing a Python List - 1000+ Python Programs
- 28: Program to Lowercase a String in Python
- Program 1: Print Hello World! - 1000+ Python Programs
- 21: Program to Delete File or Folder in Python
- 29: Program to convert Python dict to dataframe
- 33: Python Program to find the current time in India (IST)
More Posts:
- Android Toast position top - Android
- Windows 11 Notepad Find and Replace All Text Example - Windows-11
- How to switch Eclipse IDE Workspace - Eclipse
- How to come out of dquote prompt in Terminal - macOS/Linux - MacOS
- How to open TextEdit from Mac Terminal - MacOS
- Get Desc of Table using Java JDBC - Java
- How to install Google Chrome on macOS using Terminal Command - Chrome
- How to Generate SHA-256 Hash in Java With Example - Java