Python Program to Check String is a Palindrome or not


In this Python program we are going to use the slice technique. in slice technique means with the help of split we can get the individual characters of the given string and store them into another string variable.

 


Python Program to Check String is a Palindrome or not using slice

# Taking a string to check palindrome
original_string = "madam"
# Reversing the string original_string and storing in reverse_string variable using slice
reverse_string = original_string[len(original_string)::-1]
# Checking whether the given string and reversed string is same
if original_string == reverse_string:
    print("Given string is  palindrome.")
else:
    print("The given string is not palindrome.")

Output:-


Python Program to Check String is a Palindrome or not using for loop

 

# Taking a string "madam" to check palindrome
s ="madam"
# using for loop to check string is palindrome or not
for i in range(0,len(s)//2):
    if(s[i] != s[len(s)-i-1]):
        flag = False
        break
else:
    flag = True
# checking given string and reverse string is same or not
if flag:
    print("The given string is  palindrome.")
else:
    print("The given string is not  palindrome.")

 

Output:-

 


 

Leave a Reply

Your email address will not be published. Required fields are marked *