Palindrome Program in Python using Function

Palindrome program in python using function | Previously we have developed palindrome numbers in python and palindrome string in python. Now in this post, we will develop a palindrome program in python using function. 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 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:-
23532 = 23532 So, 23532 is a palindrome number.
12345 != 54321 So, 12345 is not a palindrome number.

This program completely depends on the program to find the reverse of a number. After finding the reverse of a number compare the result and the actual number if both are the same then the given number is a palindrome number else the number is not a palindrome number.

# Python program to check if number is Palindrome

def isPalindrome(n):  #user-defined function
    # calculate reverse of number
    reverse = 0
    reminder = 0
    while(n != 0):
        remainder = n % 10
        reverse = reverse * 10 + remainder
        n = int(n / 10)
    return reverse

# take inputs
num = int(input('Enter the number: '))

# calling function and display result
reverse = isPalindrome(num)
if(num == reverse):
  print(num,'is a Palindrome')
else:
  print(num,'is not a Palindrome')

Output for the different input values:-

Enter the number: 123454321
123454321 is a Palindrome

Enter the number: 12345
12345 is not a Palindrome

Enter the number: 5869685
5869685 is a Palindrome

String Palindrome in Python

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.

# Python program to check if string is Palindrome

def isPalindrome(s):  #user-defined function
    return s == s[::-1]

# take inputs
string = input('Enter the string: ')

# calling function and display result
reverse = isPalindrome(string)
if reverse:
    print(string,'is a Palindrome')
else:
    print(string,'is not a Palindrome')

Output for the different input values:-

Enter the string: refer
refer is a Palindrome

Enter the string: share
share is not a Palindrome

Enter the string: radar
radar is a Palindrome

Palindrome Program in Python using For loop

# Python program to check if string is Palindrome

def isPalindrome(s):  #user-defined function
    # Run loop from 0 to len/2 
    for i in range(0, int(len(s)/2)): 
        if s[i] != s[len(s)-i-1]:
            return False
    return True

# take inputs
string = input('Enter the string: ')

# calling function and display result
reverse = isPalindrome(string)
if reverse:
    print(string,'is a Palindrome')
else:
    print(string,'is not a Palindrome')

Output:-

Enter the string: program
program is not a Palindrome

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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