Previously we have developed palindrome numbers in python and palindrome strings in python. Now in this post, we will develop a palindrome program in python using reverse function. We will check if the given string is a palindrome string or not. Also, We will check if the given number is a palindrome number or not.
Palindrome String Program in Python using Reverse Function
Palindrome string:- If the reverse of the string is the same string then the string is called palindrome string.
Some examples of palindromic words are civic, radar, redivider, noon, level, rotor, racecar, redder, kayak, reviver, madam, and refer.
We will take a string while declaring the variables. Then, find the reverse of the string using a predefined function join(reversed()) and check if the string is equal to the reverse string using the if-else statement. Finally, the result will be displayed on the screen.
# Palindrome program in python using reverse function
# take inputs
string = input('Enter the string: ')
# find reverse using buit-in functions
rev = ''.join(reversed(string))
# check string is palindrome or not
if (string == rev):
print(string,'is a Palindrome')
else:
print(string,'is not a Palindrome')
Output for the input values test-case-1:-
Enter the string: reviver
reviver is a Palindrome
Output for the input values test-case-2:-
Enter the string: Know Program
Know Program is not a Palindrome
Palindrome Program in Python using Reverse Function
We can also take the help of a function to check the palindrome program in python. A function is a block of code that performs a specific task.
# Palindrome program in python using reverse function
def isPalindrome(s):
# find reverse using buit-in functions
rev = ''.join(reversed(s))
# check string is palindrome or not
if (s == rev):
return True
return False
# take inputs
string = input('Enter the string: ')
# calling function and display result
if (isPalindrome(string)):
print(string,'is a Palindrome')
else:
print(string,'is not a Palindrome')
Output:-
Enter the string: madam
madam is a Palindrome
Palindrome Number in Python using Reverse Function
Palindrome number:- If the Reverse of a number is equal to the same number then the number is called a palindrome number.
Example of palindrome number:-
3553 = 3553 So, 3553 is a palindrome number.
4079 != 9704 So, 4079 is not a palindrome number.
# Palindrome number in python using reverse function
# take inputs
num = input('Enter the number: ')
# find reverse using buit-in functions
rev = ''.join(reversed(num))
# check number is palindrome or not
if (num == rev):
print(num,'is a Palindrome')
else:
print(num,'is not a Palindrome')
Output for the input values test-case-1:-
Enter the number: 787
787 is a Palindrome
Output for the input values test-case-2:-
Enter the number: 8964
8964 is not a Palindrome
Palindrome Number Program in Python using Reverse Function
We can also take the help of a function to check the palindrome number in python. A function is a block of code that performs a specific task.
# Palindrome number in python using reverse function
def isPalindrome(n):
# find reverse using buit-in functions
rev = ''.join(reversed(n))
# check number is palindrome or not
if (n == rev):
return True
return False
# take inputs
num = input('Enter the number: ')
# calling function and display result
if (isPalindrome(num)):
print(num,'is a Palindrome')
else:
print(num,'is not a Palindrome')
Output:-
Enter the number: 12021
12021 is a Palindrome