Header Ads

Write Pyhton code to check a String is palindrome or not?

A palindrome is those String whose reverse is equal to the original.

Question Source

Python    Java

Code :

# Write code to check a String is palindrome or not?

def reverse(string):
rev = string[::-1]
if string == rev:
print("String is palindrome")
else:
print("String is not a palindrome")


text = input("Enter a string : ")
reverse(text)
I had used string slicing to reverse the String. Slice the string backwards by using the step value -1, to get it in reverse order.

Output :

when the String entered is abc

Enter a string : abc
String is not a palindrome

when the String entered is aba

Enter a string : aba
String is palindrome

Post a Comment

0 Comments