Palindrome String in Python

Palindrome String in Python | If the reverse of the string is the same string then the string is called palindrome string. Some examples of palindromic words are redivider, noon, civic, radar, level, rotor, kayak, reviver, racecar, redder, madam, and refer. The palindrome number is also based on the palindrome string. The reverse of a number that is equal to the same number is called a palindrome number.

Palindrome in Python

This python program using the while loop to check string is palindrome or not. We will take string while declaring the variables. Then, find the reverse of string using the while loop and check string is same to the reverse string or not using the if-else statement. Finally, the result will be displayed on the screen.

# Python program to check if string is Palindrome

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

# find reverse of string
i = string
reverse = ''
while(len(i) > 0):
    if(len(i) > 0):
        a = i[-1]
        i = i[:-1]
        reverse += a

# compare reverse to original string
if(reverse == string):
    print(string,'is a Palindrome')
else:
    print(string,'is not a Palindrome')

Output for the different input values:-

Enter the string: level
level is a Palindrome

Enter the string: python
python is not a Palindrome

Enter the string: refer
refer is a Palindrome

Python program using Slicing

We read the string and find the reverse of the string using the slicing operation. Then, the check string is the same as the reverse string or not using the if-else statement. Finally, the result will be displayed on the screen.

Syntax of slicing operation:- str(num) [::-1]

# Python program to check if string is Palindrome

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

# find reverse of string
reverse = str(string)[::-1]

# compare reverse to original string
if(reverse == string):
    print(string,'is a Palindrome')
else:
    print(string,'is not a Palindrome')

Output:-

Enter the string: program
program is not a Palindrome

We can also read input as string and then simply check for palindrome.

string = input('Enter the string: ')
if(str(string) == str(string)[::-1]):
    print(string,'is a Palindrome')
else:
    print(string,'is not a Palindrome')

Palindrome Program in Python using Recursion

We can also use the recursion technique to check if a string is a palindrome or not in Python. A technique of defining the method/function that contains a call to itself is called recursion.

# Python program to check if string is Palindrome using recursion

def isPalindrome(s):  #user-defined function
    s = s.lower()
    length = len(s)
    
    if length < 2:
        return True
    
    elif s[0] == s[length-1]:
        # Call is pallindrome form substring(1,length-1)
        return isPalindrome(s[1: length-1])
 
    else:
        return False
 
# 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: noon
noon is 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 *