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
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”)
a=int(input(“enter the number:”))
if str(a)==str(a)[::-1]:
print(“number is palindrom”)
else:
print(“number is not palindrom”)
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”)
a = input(“enter the number:”)
a_reverse = a[::-1]
if a_reverse == a:
print(“the number is Palindrome”)
else:
print(“Normal Case”)
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”)
n = input(“enter number: “)
if int(n) == int(n[::-1]):
print(“Its a palindrome”)
else:
print(“Its not a palindrome”)
#palindrome or not
n=input()
inv_n = n[::-1]
if (n==inv_n):
print(‘Palindrome’)
n=input()
inv_n = n[::-1]
if (n==inv_n):
print(‘Palindrome’)
else:
print(‘Not Palindrome’)
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”)
number=int(input(“enter number:”)
s=str(number)
if s==s[::-1]:
print(“palindrome “)
else:
print(“not a polindrome”)
x=input(“enter: “)
y=x[::-1]
if x==y :
print(“{} is a pallindrome”.format(x))
else:
print(“{} is Not a pallindrome”.format(x))
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
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”)