PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
    • Python Exercises
    • C Programming Exercises
    • C++ Exercises
  • Quizzes
  • Code Editor
    • Online Python Code Editor
    • Online C Compiler
    • Online C++ Compiler
Home » Python » Programs and Examples » Python Programs to Check Palindrome Number

Python Programs to Check Palindrome Number

Updated on: April 8, 2025 | Leave a Comment

A number that remains the same even if its digits are reversed is called a Palindromic Number. Examples are 99, 1001, 14641, 32123, etc.

There are four ways to check if a given number is a palindrome in Python. This article will explain each approach with examples.

1. How to check Palindrome Number in Python

The simple and best way is string slicing. String slicing in Python extracts a substring or entire string. It creates a new string containing the selected characters without modifying the original.

Below are the steps to check palindrome number using string reversal method:

  1. Convert number to string

    First. convert the given number to string using the str() function.
    For Example, str(12321) = "12321"

  2. Reversing the String

    Next, use the Python’s slicing technique [::-1], to create a reversed copy of the string. For Example, '12321'[::-1] = "12321"

  3. Compare the original string with its reverse

    Now, compare the original string with the reversed string. If both are the same, the number is a palindrome; otherwise, it is not. For Example, "12321" == "12321" returns True.

Code Example

number = 12321

# Convert number to string
num_str = str(number)
# Reverse the string
rev_num_str = num_str[::-1]

# Compare original string with its reverse
if num_str == rev_num_str:
    print(f"{number} is a palindrome.")
else:
    print(f"{number} is not a palindrome.")Code language: Python (python)

Output:

12321 is a palindrome.

2. Mathematical Reversal (Without String Conversion)

In this approach instead of converting the number into a string, we reverse the number mathematically and compare it with the original string. Let’s see demonstrate this with an example.

We need to use while loop, modulo operator to achieve this.

Code Example

number = 12321
original_number = 12321
reversed_number = 0

while number > 0:
    digit = number % 10  # Extract the last digit
    reversed_number = reversed_number * 10 + digit  # Append digit to reversed number
    number = number // 10  # Remove the last digit from the number

is_palindrome = original_number == reversed_number

print(f"Is {original_number} is a palindrome? : {is_palindrome}")

# Output:
# Is 12321 is a palindrome? : TrueCode language: Python (python)

Explanation

1. Initialize the variables

  • number holds the value you want to check (in this case, 12321).
  • original_number is a copy of the number so you can compare it later after reversing it.
  • reversed_number starts at 0 and will be used to build the reversed version of the number.

2. Use while loop to reverse the digits of the number

  • Extract the last digit using the modulus operation (digit = number % 10). For example, if the number is 12321, this operation gives 1 (the last digit).
  • Append the digit to reversed_number (reversed_number = reversed_number * 10 + digit):
    • Initially, reversed_number is 0, so when the first digit (1) is added, reversed_number becomes 1.
    • Each time a new digit is extracted from a number, it is appended to reversed_number by shifting the current digits to the left (multiplying by 10) and adding the new digit.
    • For example,
      after the first iteration, reversed_number = 1;
      after the second iteration, reversed_number = 12;
      and after all iterations, reversed_number = 12321.
    • Remove the last digit from the number by dividing it by 10 (number //= 10). For example, if a number is 12321, after this operation, it becomes 1232.

3. After the loop, you compare the reversed_number to original_number. If they are the same, the number is a palindrome.

3. Recursive Method

Recursion is a programming technique where a function calls itself to solve smaller instances of a problem until a base condition is met. In Python, a recursive method can also be used to check if a number is a palindrome by repeatedly comparing the first and last digits.

Code Example

# Recursive function
def is_palindrome_recursive(num, rev=0):
    # base case
    if (num == 0):
        return rev;

    # stores the reverse of a number
    rev = (rev * 10) + (num % 10);

    return is_palindrome_recursive(num // 10, rev);

# Start
number = 12321
rev_num = is_palindrome_recursive(number)
is_palindrome = number == rev_num

print(f"Is {number} a palindrome? : {is_palindrome}")

# Output:
# Is 12321 is a palindrome? : TrueCode language: Python (python)

Explanation

  • Base Case: If num == 0 This means we have processed all digits and can return the rev containing the reversed number.
  • Recursive Case: Each recursive call extracts the last digit of num and adds it to rev after shifting rev by multiplying it by 10.
  • Final Check: After recursion completes, you compare the original number with the reversed version stored in rev returned by is_palindrome_recursive() function.

Let’s work out one example step by step in detail for number = 12321

  1. First Call: is_palindrome_recursive(12321, 0)
    • rev = (0 * 10) + (12321 % 10) = 1
    • Calls is_palindrome_recursive(1232, 1)
  2. Second Call: is_palindrome_recursive(1232, 1)
    • rev = (1 * 10) + (1232 % 10) = 12
    • Calls is_palindrome_recursive(123, 12)
  3. Third Call: is_palindrome_recursive(123, 12)
    • rev = (12 * 10) + (123 % 10) = 123
    • Calls is_palindrome_recursive(12, 123)
  4. Fourth Call: is_palindrome_recursive(12, 123)
    • rev = (123 * 10) + (12 % 10) = 1232
    • Calls is_palindrome_recursive(1, 1232)
  5. Fifth Call: is_palindrome_recursive(1, 1232)
    • rev = (1232 * 10) + (1 % 10) = 12321
    • Calls is_palindrome_recursive(0, 12321)
  6. Base Case: is_palindrome_recursive(0, 12321)
    • Since num == 0, the function returns rev = 12321.
  7. Final Check:
    • After recursion, rev_num = 12321; since number == rev_num (12321 == 12321), the number is confirmed to be a palindrome.

4. Using Stack (Last In, First Out)

Below is the Python example for checking if the number is palindrome or not using stack. This method involves using a stack to reverse the digits of the number and then comparing.

def is_palindrome_stack(number):
    num_str = str(number)  # Convert number to string
    stack = []  # Initialize stack

    # Push all characters onto the stack
    for char in num_str:
        stack.append(char)

    reversed_str = ''
    # Pop characters from the stack to create reversed string
    while stack:
        reversed_str += stack.pop()

    return num_str == reversed_str  # checking original num with reversed

number = 12321
is_palindrome = is_palindrome_stack(number)

print(f"Is {number} is a palindrome? : {is_palindrome}")

# Output:
# Is 12321 is a palindrome? : TrueCode language: Python (python)

In this code

  • We converted the number into a string.
  • Stack Creation: Push all characters onto a stack (which follows Last In, First Out).
  • Reversing: Pop each character from the stack using stack.pop() function and add it to the reversed string. To create the reversed string.
  • Comparison: Compare the original string with the reversed one.

Filed Under: Programs and Examples, Python, Python Basics

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Programs and Examples Python Python Basics

All Coding Exercises:

C Exercises
C++ Exercises
Python Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 25+ questions
  • Each Quiz contains 25 MCQ
Exercises
Quizzes

Leave a Reply Cancel reply

your email address will NOT be published. all comments are moderated according to our comment policy.

Use <pre> tag for posting code. E.g. <pre> Your entire code </pre>

In: Programs and Examples Python Python Basics
TweetF  sharein  shareP  Pin

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

  Python Tutorials

  • Get Started with Python
  • Python Statements
  • Python Comments
  • Python Keywords
  • Python Variables
  • Python Operators
  • Python Data Types
  • Python Casting
  • Python Control Flow statements
  • Python For Loop
  • Python While Loop
  • Python Break and Continue
  • Python Nested Loops
  • Python Input and Output
  • Python range function
  • Check user input is String or Number
  • Accept List as a input from user
  • Python Numbers
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python Functions
  • Python Modules
  • Python isinstance()
  • Python OOP
  • Python Inheritance
  • Python Exceptions
  • Python Exercise for Beginners
  • Python Quiz for Beginners

All Python Topics

  • Python Basics
  • Python Exercises
  • Python Quizzes
  • Python File Handling
  • Python Date and Time
  • Python OOP
  • Python Random
  • Python Regex
  • Python Pandas
  • Python Databases
  • Python MySQL
  • Python PostgreSQL
  • Python SQLite
  • Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Coding Exercises

  • C Exercises
  • C++ Exercises
  • Python Exercises

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2026 pynative.com