Python program to check if a string is palindrome or not

 

Python program to check if a string is palindrome or not

Given a string, write a python function to check if it is palindrome or not. A string is said to be palindrome if the reverse of the string is the same as string. For example, “radar” is a palindrome, but “radix” is not a palindrome.
Examples: 
 

Input : malayalam
Output : Yes

Input : geeks
Output : No

 

Method #1 
1) Find reverse of string 
2) Check if reverse and original are same or not.
 


# function which return reverse of a string
 
def isPalindrome(s):
    return s == s[::-1]
 
 
# Driver code
s = "malayalam"
ans = isPalindrome(s)
 
if ans:
    print("Yes")
else:
    print("No")

Output : 

Yes

Iterative Method: This method is contributed. Run a loop from starting to length/2 and check the first character to the last character of the string and second to second last one and so on …. If any character mismatches, the string wouldn’t be a palindrome.
Below is the implementation of above approach: 



# function to check string is
# palindrome or not
def isPalindrome(str):
 
    # Run loop from 0 to len/2
    for i in range(0, int(len(str)/2)):
        if str[i] != str[len(str)-i-1]:
            return False
    return True
 
# main function
s = "malayalam"
ans = isPalindrome(s)
 
if (ans):
    print("Yes")
else:
    print("No")

Output: 
 

Yes

Method using inbuilt function to reverse a string: This method is contributed. In this method, predefined function ‘ ‘.join(reversed(string)) is used to reverse string. 
Below is the implementation of the above approach: 
 



# function to check string is
# palindrome or not
def isPalindrome(s):
     
    # Using predefined function to
    # reverse to string print(s)
    rev = ''.join(reversed(s))
 
    # Checking if both string are
    # equal or not
    if (s == rev):
        return True
    return False
 
# main function
s = "malayalam"
ans = isPalindrome(s)
 
if (ans):
    print("Yes")
else:
    print("No")

Output: 
 

Yes

Method using one extra variable: In this method user take a character of string one by one and store in an empty variable. After storing all the character user will compare both the string and check whether it is palindrome or not. 
 




# Python program to check
# if a string is palindrome
# or not
 
x = "malayalam"
 
w = ""
for i in x:
    w = i + w
 
if (x == w):
    print("Yes")
else:
    print("No")
   

Output: 
 

Yes

Method using flag: In this method user compare each character from starting and ending in a for loop and if the character does not match then it will change the status of the flag. Then it will check the status of flag and accordingly and print whether it is a palindrome or not. 
 


# Python program to check
# if a string is palindrome
# or not
st = 'malayalam'
j = -1
flag = 0
for i in st:
    if i != st[j]:
      j = j - 1
      flag = 1
      break
    j = j - 1
if flag == 1:
    print("NO")
else:
    print("Yes")

Output: 
 

Yes

    No comments:

    Post a Comment

    Your feedback is highly appreciated and will help us to improve our content.