Home » Python » Python programs

Python program to check if a string is palindrome or not

Here, we are going to learn how to check whether a given string is palindrome or not in Python programming language?
Submitted by IncludeHelp, on April 03, 2020

What is palindrome string?

A string is a palindrome if the string read from left to right is equal to the string read from right to left i.e. if the actual string is equal to the reversed string.

In the below program, we are implementing a python program to check whether a string is a palindrome or not?

Steps:

  • First, find the reverse string
  • Compare whether revers string is equal to the actual string
  • If both are the same, then the string is a palindrome, otherwise, the string is not a palindrome.

Example:

    Input: 
    "Google"
    Output:
    "Google" is not a palindrome string

    Input:
    "RADAR"
    Output:
    "RADAR" is a palindrome string

Method 1: Manual

# Python program to check if a string is 
# palindrome or not

# function to check palindrome string
def isPalindrome(string):
  result = True
  str_len = len(string)
  half_len= int(str_len/2)

  for i in range(0, half_len):
    # you need to check only half of the string
    if string[i] != string[str_len-i-1]:
      result = False
    break
  
  return result 

# Main code
x = "Google"
if isPalindrome(x):
  print(x,"is a palindrome string")
else:
  print(x,"is not a palindrome string")  

x = "ABCDCBA"
if isPalindrome(x):
  print(x,"is a palindrome string")
else:
  print(x,"is not a palindrome string")

x = "RADAR"
if isPalindrome(x):
  print(x,"is a palindrome string")
else:
  print(x,"is not a palindrome string") 

Output

Google is not a palindrome string
ABCDCBA is a palindrome string
RADAR is a palindrome string

Method 2: Slicing

# Python program to check if a string is 
# palindrome or not

# function to check palindrome string
def isPalindrome(string):
  rev_string = string[::-1]
  return string == rev_string

# Main code
x = "Google"
if isPalindrome(x):
  print(x,"is a palindrome string")
else:
  print(x,"is not a palindrome string")  

x = "ABCDCBA"
if isPalindrome(x):
  print(x,"is a palindrome string")
else:
  print(x,"is not a palindrome string")

x = "RADAR"
if isPalindrome(x):
  print(x,"is a palindrome string")
else:
  print(x,"is not a palindrome string")  

Output

Google is not a palindrome string
ABCDCBA is a palindrome string
RADAR is a palindrome string






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.








Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.