
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!
More Posts related to Python-Programs,
- Program 5: Find Sum of Two Integer Numbers - 1000+ Python Programs
- Program 8: Multiply Two Numbers - 1000+ Python Programs
- Python Program: Use NumPy to generate a random number between 0 and 1
- 25: How to rename a file using Python Program
- 22: Send Yahoo! Email using smtplib - SMTP protocol client using Python Program
- Program 15: Find String is a Palindrome or Not - 1000+ Python Programs
- 21: Program to Delete File or Folder in Python
- Program 14: Sum of Even Numbers from 1 to 100 - 1000+ Python Programs
- Program 2: Print your name using print() function - 1000+ Python Programs
- 20 - Python - Print Colors for Text in Terminal - 1000+ Python Programs
- 16: Find the largest element in a List - 1000+ Python Programs
- 33: Python Program to find the current time in India (IST)
- 23: Python Programs to concatenate two Lists
- 27: Measure Elapsed Time for a Python Program Execution
- 18: Get Sub List By Slicing a Python List - 1000+ Python Programs
- Program 10: Modulo of Two Numbers - 1000+ Python Programs
- Program 1: Print Hello World! - 1000+ Python Programs
- Program 11: Calculate Percentage - 1000+ Python Programs
- Program 12: Calculate Area and Circumference of Circle - 1000+ Python Programs
- 17: Find Factorial of a Number - 1000+ Python Programs
- 35: Python Program to find the System Hostname
- 19: Simple Calculator using built-in functions - 1000+ Python Programs
- 34: Traverse a List in Reverse Order - 1000+ Python Programming
- Program 3: Print the name of user as input - 1000+ Python Programs
- Program 9: Divide Two Numbers - 1000+ Python Programs
More Posts:
- List of Online Java compiler with console - Java
- appcompat_v7 errors after updates to API level 21 Material Theme - Android
- Create simple struts2 project using maven commands - Java
- incorrect line ending: found carriage return (\r) without corresponding newline (\n) - Android
- Switch between Python 2.x 3.x versions in Mac Terminal - MacOS
- Fix zsh: permission denied: script.sh - zsh
- Java 8: Convert Iterator to Stream Examples - Java
- How to save a file in Nano Editor and Exit - Linux