Python program to check if a string is palindrome or not

Python program to check if it is palindrome or not; Through this tutorial, i am going to show you how to check if string is palindrome or not.

Python program to check if a string is palindrome or not

  • Python Palindrome Program using Function
  • Palindrome Program in Python using for loop and function

Python Palindrome Program using Function

def isPalindrome(s):
    return s == s[::-1]
 
 
# Driver code
s = "malayalam"
ans = isPalindrome(s)
 
if ans:
    print("Yes")
else:
    print("No")

Output

Yes

Palindrome Program in Python using for loop and Function

# 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

Recommended Python Tutorials

Leave a Comment