Check Palindrome in Python

Python program to check if a string is palindrome or not

Palindrome:

If a number reads the same both forward and backward, it is called a Palindrome number. And the insane part is that it doesn’t just apply to numbers. Even if a string reads the same in both directions, it is always a Palindrome!

Examples for string palindrome

Example 1:

Input: 

given_string = madam

Output:

The given string madam is palindrome

Explanation:

Here the madam word reads same from front and end so it is palindrome

Example 2:

Input: 

given_string = vicky

Output:

The given string vicky is not palindrome

Explanation:

Here vicky and ykciv are not equal so it is not a palindrome

Examples for number palindrome

Example 1:

Input: 

given_number =12321

Output:

The given number 12321 is palindrome

Explanation:

Here the number 12321  reads same from front and end so it is palindrome

Example 2:

Input: 

given_number =12345

Output:

The given number 12345 is not palindrome

Explanation:

Here 12345 and 54321 are not equal so it is not a palindrome

Checking Palindrome in Python

There are several ways to check whether the given number is palindrome or not some of them are:

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Method #1:Using while loop to check whether the number is palindrome

Algorithm:

  • Scan the given number
  • Take another variable say duplicate_num and initialize it with given number
  • Set the variable reverse_number to 0.
  • Loop while number > 0 Loop while number > 0
  • Multiply reverse_number by 10 and add the remainder to reverse_number like below
  • reverse_number = (reverse_number * 10) + remainder
  • Divide the given number by 10 to remove the last digit.
  • If the duplicate_num and reverse_number are equal then it is palindrome.
  • Else it is not palindrome.

Below is the implementation:

# given number
given_num = 12321
# taking another variable to store the copy of original number
# and initialize it with given num
duplicate_num = given_num
# Take a variable reverse_number and initialize it to null
reverse_number = 0
# using while loop to reverse the given number
while (given_num > 0):
    # implementing the algorithm
    # getting the last digit
    remainder = given_num % 10
    reverse_number = (reverse_number * 10) + remainder
    given_num = given_num // 10
# if duplicate_num and reverse_number are equal then it is palindrome
if(duplicate_num == reverse_number):
    print("The given number", duplicate_num, "is palindrome")
else:
    print("The given number", duplicate_num, "is not palindrome")

Output:

The given number 12321 is palindrome

Method #2: Using for loop and string concatenation to check whether the number is palindrome

Approach: 

  • Scan the given number.
  • Take a empty string say revstring.
  • Convert the given number to string using str() function say strnum for this string.
  • Traverse every character of the string using for loop in reverse order using range function.
  • Add each character to revstring using string concatenation.
  • If the revstring and string_num are equal then it is palindrome.
  • Else it is not palindrome.

Below is the Implementation:

# given number
given_num = 12321
# taking empty string
reverse_string = ""
# Convert the given_num to string using str
strnum = str(given_num)
# calculating the length of string
length = len(strnum)
# Traverse the strnum string in reverse order using for loop range function
for index in range(length-1, -1, -1):
    # add the character to reverse_string using string concatenation
    reverse_string = reverse_string+strnum[index]
# if reverse_string  and strnum are equal then it is palindrome
if(reverse_string == strnum):
    print("The given number", given_num, "is palindrome")
else:
    print("The given number", given_num, "is not palindrome")

Output:

The given number 12321 is palindrome

Method #3:Using for loop and string concatenation to check whether the string is palindrome

Approach: 

  • Scan the givenstring.
  • Take a empty string say revstring.
  • Traverse every character of the string using for loop in reverse order using range function.
  • Add each character to revstring using string concatenation.
  • If the revstring and givenstring are equal then it is palindrome.
  • Else it is not palindrome.

Below is the Implementation:

# given string
given_string = "madam"
# taking empty string
reverse_string = ""
# calculating the length of string
length = len(given_string)
# Traverse the strnum string in reverse order using for loop range function
for index in range(length-1, -1, -1):
    # add the character to reverse_string using string concatenation
    reverse_string = reverse_string+given_string[index]
# if reverse_string  and strnum are equal then it is palindrome
if(reverse_string == given_string):
    print("The given string", given_string, "is palindrome")
else:
    print("The given string", given_string, "is not palindrome")

Output:

The given string madam is palindrome

Method #4:Using slicing to check whether the number is palindrome

Approach:

  • Scan the given number.
  • Convert the given number to string using str() function say strnum for this string.
  • Reverse the string using slicing
  • If the revstring and string_num are equal then it is palindrome.
  • Else it is not palindrome.

Below is the implementation:

# given number
given_num = 12321
# Convert the given_num to string using str
strnum = str(given_num)
# calculating the length of string
length = len(strnum)
# Reversing the string using slicing
reverse_string = strnum[len(strnum)::-1]
# if reverse_string  and strnum are equal then it is palindrome
if(reverse_string == strnum):
    print("The given number", given_num, "is palindrome")
else:
    print("The given number", given_num, "is not palindrome")

Output:

The given number 12321 is palindrome

Method #5: Using slicing to check whether the string is palindrome

Approach:

  • Scan the given string.
  • Reverse the string using slicing
  • If the revstring and string are equal then it is palindrome.
  • Else it is not palindrome.

Below is the implementation:

# given string
given_string = "madam"
# calculating the length of string
length = len(given_string)
# Reversing the string using slicing
reverse_string = given_string[len(given_string)::-1]
# if reverse_string  and strnum are equal then it is palindrome
if(reverse_string == given_string):
    print("The given string", given_string, "is palindrome")
else:
    print("The given string", given_string, "is not palindrome")

Output:

The given string madam is palindrome

Related Programs: