Python Program to Check a Number or String is Palindrome
In this tutorial, we are going to see how to write a palindrome program in Python using recursion. A number is a palindrome if it is written in the same way after its inversion.
Example:
232, 191, 22022, 111, 666, 12012
The program’s logic
- Get the number/string to check
- Keep the number/string in a temporary variable
- Reverse the number/string
- Compare temporary number/string with the reversed number/string
- If the two numbers/strings are the same, display “xxxx is a palindrome”
- Otherwise, print “xxxx is not a palindrome”
Python Program to Check a Number or String is Palindrome
var = input(("Enter a value: "))
if(var == var[::-1]):
print("The input is a palindrome")
else:
print("The input is not a palindrome")
Output:
Enter a value: 232 The input is a palindrome
Python Program to Check a Number or String is Palindrome Using Function
def check_palindrome(v):
reverse = v[::-1]
if (v == reverse):
return True
return False
var = input(("Enter a value: "))
if(check_palindrome(var)):
print("The input is a palindrome")
else:
print("The input is not a palindrome")
Output:
Enter a value: 232 The input is a palindrome
Palindrome Program In Python Using Recursion Function
def check_palindrome(v):
if len(v) < 1:
return True
else:
if v[0] == v[-1]:
return check_palindrome(v[1:-1])
else:
return False
var = input(("Enter a value: "))
if(check_palindrome(var)):
print("The input is a palindrome")
else:
print("The input is not a palindrome")
Output:
Enter a value: 232 The input is a palindrome




