Skip to content

Python Program to check if a string is Palindrome or not (in 3 ways)

Python Program to check if a string is Palindrome or not

In this tutorial we will write a Python Program to check if a string is Palindrome or not . Before reading further we recommend to read about below tutorial-

What is a palindrome string

If reverse string of a string is same as the given string then the string is a Palindrome string .

First we will take a string level . After that we will reverse the string . As the reversed string of level is also level so level is a Palindrome string .

We can check if a string is palindrome or not in Python using various ways . All the ways are described below-

Python Program to check if a string is Palindrome or not using string slicing

Steps

  • First we will convert the string into lowercase using lower() method to avoid any case-sensitive issue .
  • Then we will reverse the string using string slicing and store in a new variable .
  • After that we will compare the original string with the reversed string .
# defining a function
def is_palindrome(mystring):
    mystring=mystring.lower()
    reverse=mystring[::-1]
    print(mystring==reverse)

# calling the function
is_palindrome("level")
is_palindrome("Rajkumar")
True
False
  • In the above code first we have defined a function named is_palindrome() .
  • After that we have called the function with "level" string . As reverse of "level" is also "level" so program has printed True in the output .
  • After that we have called the function again with "Rajkumar" string . First the program has converted "Rajkumar" to "rajkumar" . As reverse of "rajkumar" is "ramukjar" which is not "rajkumar" so program has printed False in the output .

Python Program to check if a string is Palindrome or not using for loop

Steps

  • First we will convert the string into lowercase using lower() method to avoid any case-sensitive issue .
  • Then we will create a new variable with an empty string and added all the character of the string from backward using for loop .
  • After that we will compare the original string with the reversed string .
# defining a function
def is_palindrome(mystring):
    mystring=mystring.lower()
    reverse = ""
    for i in range(len(mystring)-1,-1,-1):
        reverse+=mystring[i]        
    print(mystring==reverse)

# calling the function
is_palindrome("level")
is_palindrome("Rajkumar")
True
False
  • In the above code first we have defined a function named is_palindrome() .
  • After that we have called the function with "level" string . As reverse of "level" is also "level" so program has printed True in the output .
  • After that we have called the function again with "Rajkumar" string . First the program has converted "Rajkumar" to "rajkumar" . As reverse of "rajkumar" is "ramukjar" which is not "rajkumar" so program has printed False in the output .

Python Program to check if a string is Palindrome or not using while loop

Steps

  • First we will convert the string into lowercase using lower() method to avoid any case-sensitive issue .
  • Then we will create a new variable with an empty string and added all the character of the string from backward using while loop .
  • After that we will compare the original string with the reversed string .
# defining a function
def is_palindrome(mystring):
    mystring=mystring.lower()
    reverse = ""
    i=len(mystring)-1
    while i>-1:
        reverse+=mystring[i]   
        i-=1
    print(mystring==reverse)

# calling the function
is_palindrome("level")
is_palindrome("Rajkumar")
True
False
  • In the above code first we have defined a function named is_palindrome() .
  • After that we have called the function with "level" string . As reverse of "level" is also "level" so program has printed True in the output .
  • After that we have called the function again with "Rajkumar" string . First the program has converted "Rajkumar" to "rajkumar" . As reverse of "rajkumar" is "ramukjar" which is not "rajkumar" so program has printed False in the output .

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .

Leave a Reply