Palindrome Program in Python

Check Whether or Not the Number is a Palindrome in Python

Given an integer number as an input, the objective is to check Whether or not the number is a palindrome. Therefore, we write a code to Check Whether or Not the Number is a Palindrome in Python Language.

Example
Input : 1221
Output : Palindrome
Check Whether ot not the Number is a Palindrome in Python

Check Whether or Not the Number is a Palindrome in Python Language

Given an integer input the objective is to check whether or not the given integer number as an input is palindrome or not.

For a number to be a Palindrome, the number must be the same when reversed. If the number doesn’t match the reverse of itself, the number is not a Palindrome.

  • Method 1:  Using Simple Iteration.
  • Method 2: Using String Slicing.
  • Method 3: Using Recursion
  • Method 4:  Using Character matching
  • Method 5: Using Character matching updated
  • Method 6: Using Built-in reversed function
  • Method 7:  Building reverse one char at a time
  • Method 8: Using Flag and backward reading
  • Method 9: Bonus using backward slicing

We’ll discuss the above-mentioned methods in detail in the sections below. Don’t forget to check the blue box mentioned below for better understanding of the problem.

Number is Palindrome or not in C

Method 1: Using Simple Iteration

Working

For a given integer variable, we perform the following operations,

  • Run a While loop with condition as temp number < 0.
    • Using modulo operator, extract the last digit from the number.
    • Using the formula reverse = reverse * 10 + remainder , we’ll keep updating the reverse variable.
    • Using the divide operator, we’ll shorten the number.
  • Check if the reversed number matches the original number.

Let’s implement the above mentioned code in Python Language.

Python Code

Run
num = 1221
temp = num
reverse = 0
while temp > 0:
    remainder = temp % 10
    reverse = (reverse * 10) + remainder
    temp = temp // 10
if num == reverse:
  print('Palindrome')
else:
  print("Not Palindrome")

Output

 Palindrome

Method 2: Using String Slicing

Working

In this method we’ll convert the number to string format and reverse the string. We’ll check if the reversed number matches the original one at the end.

For an integer input number, we perform the following operations,

  • Convert the number to string format using str() function.
  • Using string slicing, reverse the number.
  • Check if the reversed number matches the original number.

Let’s implement the above mentioned logic in Python Language.

Python Code

Run
num = 1234
reverse = int(str(num)[::-1])

if num == reverse:
  print('Palindrome')
else:
  print("Not Palindrome")

Output

Not Palindrome

Method 3: Using Recursion

Working

In this method we’ll use recursion. To know more about recursion, check our page, Recursion in Python.

For a given integer number as input we perform the following,

  • Define a function recurrev() which takes the number and the reverse variable as arguments.
  • Set the base case as number == 0 and the step recursive call as recurrev(number//10,reverse).
  • Check if the returned value matches the original number.

Let’s implement the above mentioned logic in Python Language.

Python Code

Run
def recurrev(number, rev):
    if number == 0:
        return rev

    remainder = int(number % 10)
    rev = (rev * 10) + remainder

    return recurrev(int(number / 10), rev)


num = 12321
reverse = 0
reverse = recurrev(num, reverse)

print(str(num) + " is: ", end="")
print("Palindrome") if reverse == num else print("Not Palindrome")

Output

 Palindrome

Method 4: Using Character matching

Working

For string str iterate on the whole check if we find any condition such that – 

  • str[i] != str[len(str) – i – 1]
  • If yes then its not a palindrome

Basically, we are checking ‘i’th character is the same as ‘i’th character from the end or not

Run
def checkPalindrome(str):

    # check if str[i] is same as str[len(str) - i - 1]
    # for whole string
    for i in range(0, len(str)):

        # Basically, we are checking i-th character is
        # same as i-th character from the end or not
        if str[i] != str[len(str) - i - 1]:
            return False

    return True


# main function
s = "kayak"

print("Palindrome") if checkPalindrome(s) else print("Not Palindrome")

Output

 Palindrome

Method 5: Using Character matching (Updated)

Working

The logic is same as the previous method with the only difference that –

Rather than iterating on the whole string, we iterate on half of the string input.

Since, if the string is a palindrome, the first half will be the same as the second half from the end.

Run
# we do not need to check the whole string
# only till the mid of string
# as if it palindrome the first half == second half of string when read backwards
def checkPalindrome(str):

    # Run loop from 0 to len/2
    mid = int(len(str) / 2)

    for i in range(0, mid):
        if str[i] != str[len(str) - i - 1]:
            return False

    return True


# main function
s = "kayak"

print("Palindrome") if checkPalindrome(s) else print("Not Palindrome")

Output

 Palindrome

Method 6: Using In-Built reversed function

Working

Use the following –

reverse = ”.join(reversed(str))

Run
def checkPalindrome(str):
    # using inbuilt reversed function
    reverse = ''.join(reversed(str))

    if str == reverse:
        return True

    return False


# main function
s = "kayak"

print("Palindrome") if checkPalindrome(s) else print("Not Palindrome")

Output

 Palindrome

Method 7: Building reverse one char at a time

The explanation is given in the comments section of the code below –

Run
string = "123"
# this will automatically generate reverse
rev = ""
for char in string:
    rev = char + rev

print("Palindrome") if string == rev else print("Not Palindrome")

print("string: " + str(string))
print("rev: " + str(rev))

Output

Not Palindrome
string: 123
rev: 321

Method 8: Using Flag

The explanation is given in the comments section of the code below –

Run
string = "radar"

j = -1
flag = 0
for char in string:
    # char starts from index 0
    # string[j] forces to read from end
    # bcz negative index are read from end
    if char != string[j]:
        flag = 1
        break
    j = j - 1

print(string + " is : ", end="")
print("Not Palindrome") if flag else print("Palindrome")

Output

radar is : Palindrome

Method 8: Bonus using backward slicing

We use backward looping/slicing in a for loop here

Run
str1 = "radar"
n = len(str1)
c = []
for i in range(n - 1, -1, -1):
    c.append(str1[i])

rev = "".join(c)

print(str1 + " is: ", end="")
if str1 == rev:
    print("Palindrome")
else:
    print("Not Palindrome")

Output

radar is : Palindrome

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

27 comments on “Palindrome Program in Python”


  • asatiayush0

    number = [int(d) for d in input(“Enter number: “)]
    temp = “”.join(str(d) for d in number)
    if int(temp) == int(“”.join(str(d) for d in reversed(number))):
    print(“Number is Palindrome “)
    else:
    print(“Number is not Palindrome”)


  • Prashant

    a=int(input(“enter the number:”))
    if str(a)==str(a)[::-1]:
    print(“number is palindrom”)
    else:
    print(“number is not palindrom”)


  • PRERNA

    n=input(“enter a number”)
    rev=””
    for i in n:
    rev=i+rev
    if(rev==n):
    print(“this is Palindrome number”)
    else:
    print(“the is not Palindrome number”)


  • rajat

    a = input(“enter the number:”)
    a_reverse = a[::-1]
    if a_reverse == a:
    print(“the number is Palindrome”)
    else:
    print(“Normal Case”)


  • akash

    string=input(“enter the string: “)
    rev_string=string[::-1]
    print(“reversed string”)
    if string==rev_string:
    print(“it is a palindrome “)
    else:
    print(“not a palindrome”)


  • gaurav

    n = input(“enter number: “)
    if int(n) == int(n[::-1]):
    print(“Its a palindrome”)
    else:
    print(“Its not a palindrome”)


  • IPE2K17

    #palindrome or not
    n=input()
    inv_n = n[::-1]
    if (n==inv_n):
    print(‘Palindrome’)


    • Abhav

      n=input()
      inv_n = n[::-1]
      if (n==inv_n):
      print(‘Palindrome’)
      else:
      print(‘Not Palindrome’)


  • Raman

    a=input()
    n=len(a)
    c=[]
    for i in range(n-1,-1,-1):
    c.append(a[i])
    print(int(a[i]),end=””)

    d=””.join(c)
    print()

    if(int(a)==int(d)):
    print(“palindrome”)
    else:
    print(“Not Palindrome”)


  • Gudala

    number=int(input(“enter number:”)
    s=str(number)
    if s==s[::-1]:
    print(“palindrome “)
    else:
    print(“not a polindrome”)


  • Pandimurugan

    number = int(input())
    reverse = str(number)
    if number == int(reverse[::-1]):
    print(“It is a palindrome”)
    else:
    print(“it is not a palindrome”)

    This is the correct one


  • Pandimurugan

    number = int(input(“Enter a number: “)
    reverse = str(int( : :-1)
    if number == reverse:
    print(“It is a palindrome”)
    else:
    print(“it is not a palindrome”)