Mastering Python (92 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (26 Blogs)
  • Data Science (20 Blogs)
  • Mastering Python (86 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

Palindrome in Python: How to Check a Number or String is Palindrome?

Last updated on Dec 05,2024 402.3K Views

Aayushi Johari
A technophile with a passion for unraveling the intricate tapestry of the... A technophile with a passion for unraveling the intricate tapestry of the tech world. I've spent over a decade exploring the fascinating world of...
1 / 11 Blog from Python Programs

As kids, it was fun reading reverse strings and when we grew up a little we learned that strings reading same either ways are called palindromes. Curiosity dint leaves us there, so we wanted our machines to learn what are palindromes and for all Python lovers, no other language can do it in a better way. If you are a python lover and a coding enthusiast, read along to learn how to check for a Palindrome number in Python using while loop and in-built functions.

Python For Data Science Full Course – 9 Hours | Data Science With Python | Python Training | Edureka

This Edureka video on the ‘Python For Data Science Full Course’ will help you learn Python for Data Science including all the relevant libraries. Following are the topics discussed in this Python for Data Science tutorial

when checking for if a number is Palindrome or not with Python, we will be using while loop and in-built functions. For beginners, Edureka has a 360° Python course page  which will help you learn Python from scratch even if you don’t have much coding knowledge.

What is a Palindrome?

A palindrome is nothing but any number or a string which remains unaltered when reversed.

Example: 12321
Output:
Yes, a Palindrome number

Example: RACECAR
Output: Yes, a Palindrome string

It is evident that letters form mirror images on reversal.

Now that you understood the concept, let’s simply dive into a program to check palindrome in Python.

 

Palindrome Algorithm

Let’s see how we check for Palindromes in Python

To check a Palindrome in Python, the program should check whether a given string is the same when read forwards and backward.

Consider the following Python program

string = input(“Enter a string: ”)
if string == string[::-1]:
print(“The string is a palindrome”)
else:
print(“Not a palindrome”)

First, it asks the user to enter a string and stores it.

Then, it checks if the string is same as its reverse.

If it’s equal to its reverse, the program prints “The string is a palindrome.”

Otherwise, it prints “The string is not a palindrome.”

Palindrome Program using Native Approach

The native approach is one of the easiest way to check the Palindrome in Python. Let’s jump onto an example to check whether a given input string is palindrome or not.

string = input(“Enter a string: ”)
if string == string[::-1]:
print(“The string is a palindrome”)
else:
print(“Not a palindrome”)

Enter a string: level
Output: The string is a palindrome

Moving ahead in Python palindrome program examples, let’s see how to check a string whether it’s a palindrome using the Iterative method.

Palindrome Program using Iterative Method

String = input(“Enter a string: ”)
is_palindrome = True
for i in range (0, int (len(string) / 2)):
if string[i] != string[len(string) - i - 1]:
is_palindrome = False
break
if is_palindrome:
print(“The string is palindrome”)
else:
print(“Not a palindrome”)

Enter a string: 1221
Output: The string is a palindrome

Now, let’s see how to check a string whether it’s a palindrome using While loop.

Palindrome Program using While Loop

This is one of the easiest programs to find Palindrome program using while loop in Python Programming. Let’ dive into an example to check whether a given input is a palindrome or not.

num=int(input("Enter a number:"))
temp=num
rev=0
while(num>0):
    dig=num%10
    rev=rev*10+dig
    num=num//10
if(temp==rev):
    print("The number is palindrome!")
else:
    print("Not a palindrome!")

Output:

Enter a number:121
The number is palindrome!

Moving ahead in Python palindrome program examples, let’s us see how to check a string whether its a palindrome or not using built-in functions.

Palindrome Program( String) using inbuilt Method

 

string=input(("Enter a string:"))
if(string==string[::-1]):
      print("The string is a palindrome")
else:
      print("Not a palindrome")

Output:

Palindrome in Python - Edureka

Explanation: In the above program, first take input from the user (using input OR raw_input() method) to check for palindrome. Then using slice operation [start:end:step], check whether the string is reversed or not. Here, step value of -1 reverses a string. If yes, it prints a palindrome else, not a palindrome.

Moving ahead, let’s see how to check a string whether it’s a palindrome using inbuilt functions to reverse a string.

Palindrome Program using Inbuilt Method

string = input("Enter a string: ")
reversed = ''.join(reversed(string))
if string == reversed:
print("The string is a palindrome")
else:
print("Not a palindrome")
Enter a string: 1212

Output: Not a palindrome

Now, it’s time to see how to check a string whether it’s a palindrome using One Extra Variable.

Palindrome Program using One Extra Variable

string = input("Enter a string: ")
w = ""
for i in string:
w = i + w
if string == w:
print("The string is a palindrome")
else:
print("Not a palindrome")
Enter a string: civic

Output: The string is a palindrome

Let’s see how to check a string whether it’s a palindrome using flag.

Check if a String is Palindrome or not using flag

string = input("Enter a string: ")
index_backwards = -1
is_not_palindrome = 0
for index_forward in range(len(string)):
if string[index_forward] != string[index_backwards]:
is_not_palindrome = 1
break
index_backwards -= 1
if is_not_palindrome == 1:
print("The string is a palindrome")
else:
print("Not a palindrome")

Enter a string: 2003
Output: Not a palindrome

Moving ahead, we’ll see how to check a string whether it’s a palindrome using recursion.

Check if a String is Palindrome or not using recursion

string = input("Enter a string: ")
string = string.lower()
l = len(string)
is_palindrome = True
while l > 1:
if string[0] == string[-1]:
string = string[1:-1]
l = len(string)
else:
is_palindrome = False
break
if is_palindrome:
print("The string is a palindrome")
else:
print("Not a palindrome")

Enter a string: rotor
Output: The string is a palindrome

FAQs

  • What is an example of a palindrome in Python?

A palindrome is a sequence of characters which reads the same backward as forward

In Python, we can create a simple program to check if it’s a palindrome.

string = input(“Enter a word: ”)
if string == string[::-1]:
print(“The string is a palindrome”)
else:
print(“Not a palindrome”)
In this example, the word "radar" is a palindrome.

 

  • How do you find the palindrome between two numbers in Python?

 

To find the palindrome numbers between two numbers, we can write a loop to check each number in the range and see if it reads the same forward and backward.

Here’s a simple example

start = 100
end = 200
palindromes = []
for number in range(start, end + 1):
if str(number) == str(number)[::-1]:</span>
palindromes.append(number)
print("Palindromes: ",palindromes)
  1. We start at 100 (start = 100).
  2. We end at 200 (end = 200).
  3. We create an empty list called palindromes.
  4. We loop over each number.
  5. If it’s a palindrome, we add it to the list.
  6. We print the list.

Upskill for Higher Salary with Python Programming Courses

Course Name

Upcoming Batches

Fees

Python Certification Course20th April 2024 (Weekend Batch)₹17,795
Python Certification Course18th May 2024 (Weekend Batch)₹15,125
This brings us to the end of this article where we have learned how to find palindrome in Python. I hope you are clear with all that has been shared with you in this tutorial.

Make sure you practice as much as possible and revert your experience.  

 

Top 10 Trending Technologies to Learn in 2025 | Edureka

This video talks about the Top 10 Trending Technologies in 2025 that you must learn.

Got a question for us? Please mention it in the comments section of this “Palindrome program in Python” blog and we will get back to you as soon as possible, or you can join our Data science with Python course.

Explore top Python interview questions covering topics like data structures, algorithms, OOP concepts, and problem-solving techniques. Master key Python skills to ace your interview and secure your next developer role.

Discover your full abilities in becoming an AI and ML professional through our Artificial intelligence course. Learn about various AI-related technologies like Machine Learning, Deep Learning, Computer Vision, Natural Language Processing, Speech Recognition, and Reinforcement learning.

Learn about Jupyter Notebook Cheat Sheet

Upcoming Batches For Data Science with Python Certification Course
Course NameDateDetails
Data Science with Python Certification Course

Class Starts on 15th February,2025

15th February

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Palindrome in Python: How to Check a Number or String is Palindrome?

edureka.co