Palindrome Program in Python using String Functions

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 string functions. We will check the given number is a palindrome number or not. Also, We will check the given string is a palindrome string or not.

Palindrome Number in Python using String Functions

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:-
96569 = 96569 So, 96569 is a palindrome number.
25466 != 66452 So, 25466 is not a palindrome number.

We will take integer numbers while declaring the variables. Then, find the reverse of the number using a predefined function join(reversed()) and check if the number is equal to the reverse number using the if-else statement. Finally, the result will be displayed on the screen.

# Palindrome number in python using string functions

# 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: 2552
2552 is a Palindrome

Output for the input values test-case-2:-

Enter the number: 45645
45645 is not a Palindrome

Palindrome Number Program in Python using String Functions

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 string functions

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: 45654
45654 is a Palindrome

Palindrome Program in Python using String Functions

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.

# Palindrome program in python using string functions
    
# 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: racecar
racecar is a Palindrome

Output for the input values test-case-2:-

Enter the string: Know Program
Know Program is not a Palindrome

Palindrome String Program in Python using String 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 string functions

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: refer
refer is a Palindrome

Leave a Comment

Your email address will not be published. Required fields are marked *