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 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.
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
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
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
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
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.
# 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))
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 –
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 –
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
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
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python
n = int(input(“enter a number: “))
x = str(n)[::-1]
y = int(x)
print(y)
if n==y:
print(‘n is pallindrome’)
else:
print(‘not pallindrome’)
a=int(input())
temp=a
rev=0
while(a>0):
rem=a%10
rev=(rev*10)+rem
a=a//10
if(temp==rev):
print(‘it is a Palindrome ‘,rev,’ ,’,temp)
else:
print(‘it is not a Palindrome ‘,rev,’ ,’,temp)
x = input(“Enter a Number: “)
a = str(x)[::-1]
if x == a:
print(“{}”.format(x), “is Palindrome”)
else:
print(“{}”.format(x), “is not Palindrome”)
x = input()
z = list(x)
z.reverse()
a = “”.join(z)
if x == a:
print(“{} is palidrome”.format(x))
else:
print(“{} is not palidrome”.format(x))
a = int(input(“Enter The Number: \n”))
b = (str(a)[::-1])
print(b)
if a==int(b):
print(“The number is palindrome”)
else:
print(“The number is not palindrome”)
#Finding palindrome or not
a = list(map(str,input()))
print(“Palindrome” if a == a[::-1] else “not palindrome”)
num =str(input())
print(“The number is “,num)
pal = num[::-1]
if num == pal:
print(“The entered no. is Pal”)
else:
print(“The entered no. is not Pal”)
n = input(“enter:”)
p = n[::-1]
if n == p:
print(“Palindrome found”,n)
else:
print(“Not Palindrome”)
#palindrome or not
n=input()
if n==n[::-1]:
print(“Palindrome”)
else:
print(“No Palindrome”)
m=input(“enter word”)
n=m[::-1]
if n==m:
print(“palindrome”)
else:
print(“not”)
count=0
st=[str(n) for n in input() ]
l=len(st)
for i in range(0, int(l/2)+1):
if(st[i]==st[l-1]):
count=count+1
l=l-1
break
if (count>0):
print(“palindrome”)
else:
print(“not palindrome”)
x = input ()
y = str(x)[::-1]
if x == y:
print(“Palindrome”)
else :
print(“Sorry”)