Check a Palindrome Number in Python Using a Function

Recently, while working on a small Python project for a client in New York, I had to verify if certain numeric IDs were palindrome numbers.

At first, it seemed like a simple task, but I quickly realized that writing a clean, reusable Python function would make my workflow much easier.

In this tutorial, I’ll show you how to check a palindrome number in Python using a function.
I’ll also share a few different methods, from simple string reversal to mathematical approaches, so you can choose the one that fits your coding style.

What Is a Palindrome Number?

Before we start coding, let’s understand what a palindrome number is.

A palindrome number is a number that remains the same when its digits are reversed. For example, 121, 1331, and 12321 are palindrome numbers because reversing them gives the same number.

In Python, checking if a number is a palindrome can be done in multiple ways. We can use string operations, loops, or even recursion, and I’ll show you all of them step-by-step.

Method 1 – Check Palindrome Number in Python Using String Reversal

This is the simplest and most beginner-friendly method. We’ll convert the number into a string, reverse it, and then compare it to the original string.

Here’s how I usually do it in Python:

def is_palindrome(num):
    # Convert the number to a string
    str_num = str(num)

    # Reverse the string using slicing
    reversed_num = str_num[::-1]

    # Compare the original and reversed strings
    if str_num == reversed_num:
        return True
    else:
        return False

# Example usage
number = 12321
if is_palindrome(number):
    print(f"{number} is a palindrome number.")
else:
    print(f"{number} is not a palindrome number.")

In this approach, I used Python’s slicing feature ([::-1]) to reverse the string. It’s concise, efficient, and works perfectly for both small and large numbers.

If you run this code, you’ll see the output:

12321 is a palindrome number.

You can see the output in the screenshot below.

palindrome number

This method is great for quick checks, but it involves converting integers to strings, which might not be ideal in all scenarios.

Method 2 – Check Palindrome Number in Python Without Converting to String

Sometimes, you might want to avoid using string conversions, especially when working with numeric data in performance-heavy applications. In such cases, you can reverse the number mathematically.

Here’s how I do it:

def is_palindrome_number(num):
    original_num = num
    reversed_num = 0

    # Reverse the number using arithmetic operations
    while num > 0:
        digit = num % 10
        reversed_num = reversed_num * 10 + digit
        num = num // 10

    # Compare the original and reversed numbers
    return original_num == reversed_num

# Example usage
number = 4554
if is_palindrome_number(number):
    print(f"{number} is a palindrome number.")
else:
    print(f"{number} is not a palindrome number.")

In this method, I extract each digit using the modulus operator (%) and rebuild the number in reverse order. This approach is pure math, no strings involved, and it’s a great way to practice logical thinking in Python.

When you run this code, you’ll get:

4554 is a palindrome number.

You can see the output in the screenshot below.

palindrome number in python

I often use this method when working with numeric datasets in Python, especially when I need to validate large lists of numbers quickly.

Method 3 – Check Palindrome Number in Python Using Recursion

If you like recursion as much as I do, you’ll enjoy this approach. Recursion allows you to break down the problem into smaller parts until you reach a base case.

Here’s a simple recursive function to check palindrome numbers in Python:

def is_palindrome_recursive(num, temp=None):
    # Initialize temp with original number
    if temp is None:
        temp = num

    # Base case: if num becomes 0
    if num == 0:
        return 0

    # Recursive call to reverse the number
    reverse = (num % 10) * (10 ** (len(str(temp)) - len(str(num)))) + is_palindrome_recursive(num // 10, temp)
    return reverse

# Helper function to check palindrome
def check_palindrome(num):
    return num == is_palindrome_recursive(num)

# Example usage
number = 1221
if check_palindrome(number):
    print(f"{number} is a palindrome number.")
else:
    print(f"{number} is not a palindrome number.")

You can see the output in the screenshot below.

what is palindrome number

This approach uses recursion to build the reversed number step-by-step. It’s not the most efficient method, but it’s a great exercise to understand how recursion works in Python.

Method 4 – Check Palindrome Number in Python Using a Lambda Function

Sometimes, I like to keep my code compact, especially for quick one-liners. Python’s lambda functions are perfect for that.

Here’s a quick way to check a palindrome number using a lambda expression:

is_palindrome = lambda num: str(num) == str(num)[::-1]

# Example usage
number = 98789
if is_palindrome(number):
    print(f"{number} is a palindrome number.")
else:
    print(f"{number} is not a palindrome number.")

You can see the output in the screenshot below.

palindrome number python

This one-liner is incredibly handy when I’m working in Jupyter notebooks or quick Python scripts. It’s not only clean but also very readable for anyone familiar with Python.

Method 5 – Check Palindrome Number in Python Using List Reversal

Another interesting approach is to convert the number into a list of digits. Once you have a list, you can reverse it and compare it to the original.

Here’s how I do it:

def is_palindrome_list(num):
    digits = [int(d) for d in str(num)]
    return digits == digits[::-1]

# Example usage
number = 1001
if is_palindrome_list(number):
    print(f"{number} is a palindrome number.")
else:
    print(f"{number} is not a palindrome number.")

In this method, I used a list comprehension to convert each digit into a list element. It’s a Pythonic and elegant way to handle numeric data structures.

Real-World Example – Check Palindrome ZIP Codes in the USA

Let’s take a practical example. Suppose you’re analyzing ZIP codes across the United States and want to find which ones are palindrome numbers.

Here’s a quick Python snippet to do that:

zip_codes = [33133, 90210, 12321, 60606, 75057, 94149, 99599]

def find_palindrome_zips(zip_list):
    return [z for z in zip_list if str(z) == str(z)[::-1]]

palindrome_zips = find_palindrome_zips(zip_codes)
print("Palindrome ZIP Codes:", palindrome_zips)

When you run this code, you’ll get:

Palindrome ZIP Codes: [33133, 12321, 60606, 94149, 99599]

This is a great example of applying Python logic to real-world data, something I often do in data analysis projects.

Best Practices for Writing Palindrome Functions in Python

After working with Python for over a decade, here are a few best practices I always follow:

  1. Keep functions modular – Each function should do one thing well.
  2. Avoid unnecessary conversions – Use numeric methods when possible.
  3. Test edge cases – Try numbers like 0, 10, and 1001.
  4. Use descriptive names – Make your code readable for others.
  5. Document your functions – Add short docstrings explaining what each function does.

These practices not only make your Python code cleaner but also easier to maintain and debug.

Conclusion

So that’s how I check palindrome numbers in Python using functions. We explored several methods, from simple string reversal to recursion and lambda expressions.

Each method has its own advantages, and the best one depends on your use case. If you’re working with small numbers or quick scripts, string reversal is perfect. For performance-sensitive tasks, the arithmetic method works best.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.