In this Python tutorial, we will understand how to write a Python Program to check if it is a palindrome or not. In addition, we will about palindromes and the program to find palindromes between 1 to 100.
What is Palindrome in Python
A palindrome is a word, phrase, number, or other sequence of characters that reads the same backward as forward, ignoring spaces, punctuation, and capitalization. Examples of palindromes include “madam”, “racecar”, and “level”.
Palindrome Function in Python
In Python, we can write a function to check whether a given string is a palindrome. To do this, we first normalize the string by converting it to lowercase. This is because the function should consider “Madam” and “madam” as identical.
Next, we’ll reverse the string and compare it with the original string. If they match, the string is a palindrome; otherwise, it isn’t. Let’s see how this works with actual Python code:
def is_palindrome(s): s = s.lower() # Convert to lower case reversed_s = s[::-1] # Reverse the string return s == reversed_s # Compare original and reversed strings print(is_palindrome('racecar')) print(is_palindrome('Python'))
The Python code creates a function
is_palindrome(s) to determine if a string
s is the same forwards and backward, ignoring the case. The function converts the string to lowercase, reverses it, and checks if the reversed string equals the original. ‘racecar’ returns True (it is a palindrome), while ‘Python’ returns False (not a palindrome).
Palindrome Function in Python to Ignore Spaces and Punctuation
However, the function above only works for single-word palindromes without spaces or punctuation. If you need a function to check whether a phrase is a palindrome, ignoring spaces, punctuation, and case, we can modify the function as follows:
import string def is_palindrome(s): # Remove punctuation and convert to lower case s = ''.join(c for c in s.lower() if c not in string.punctuation) # Remove spaces s = s.replace(' ', '') # Reverse the string reversed_s = s[::-1] # Compare original and reversed strings return s == reversed_s print(is_palindrome('Able was I ere I saw Elba')) print(is_palindrome('A man, a plan, a canal, Panama'))
This Python script defines a function
is_palindrome(s) that checks if a phrase
s is a palindrome, ignoring case, spaces, and punctuation.
The function converts the phrase to lowercase, removes punctuation and spaces, then checks if the resulting string reads the same forwards and backward. If it does, the phrase is a palindrome.
Two phrases are then tested using this function: “Able was I ere I saw Elba” and “A man, a plan, a canal, Panama”. Both should return True as they are palindromes when disregarding case, spaces, and punctuation.
Palindrome Number between 1 to 100 in Python
To find all palindrome numbers between 1 and 100 in Python, we can write a loop that iterates over these numbers, and then use a function to check if each number is a palindrome. Here’s a simple script that does this:
def is_palindrome(n): # Convert the number to a string str_n = str(n) # Reverse the string reversed_str_n = str_n[::-1] # Compare the original and reversed strings return str_n == reversed_str_n # Iterate over numbers from 1 to 100 for i in range(1, 101): if is_palindrome(i): print(i)
The Python script defines a function named
is_palindrome which checks if a given number is a palindrome. A number is considered a palindrome if it reads the same backward as forward. This function converts the number into a string, reverses this string, and then checks if the reversed string is equal to the original string.
The script then runs a loop over numbers from 1 to 100. For each number, it checks if the number is a palindrome using the
is_palindrome function. If the number is a palindrome, it gets printed out.
As we can see, Python’s powerful string processing features allow us to easily write a function to check whether a string is a palindrome. The flexibility of the language allows us to customize this function to account for case sensitivity, spaces, and punctuation. Even it also works for numbers.
You may also like to read:
- Create a tuple in Python
- Python Program for even or odd
- Python Keywords with examples
- Constructor in Python
- Python Anonymous Function
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.