A string is said to be a palindrome if the string read from left to right is equal to the string read from right to left.
method 1:-
# 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
method 2:
def isPalindrome(string):
left_pos = 0
right_pos = len(string) - 1
while right_pos >= left_pos:
if not string[left_pos] == string[right_pos]:
return False
left_pos += 1
right_pos -= 1
return True
print(isPalindrome('12121'))
output:-
True
method 3:-
x = "lol"
w = ""
for i in x:
w = i + w
if (x == w):
print("Yes")
else:
print("No")
output:-
Yes
method 4:-
def check(x) :
if x[ -1 : - len(x) - 1 : - 1] == x:
print ("String is a palindrome.")
else :
print ("String is not a palindrome ")
string = input ("Enter a String :- ")
check(string)
output:-
Enter a String :- lolPlease enter your own String : lol
This is a Palindrome String
>>>
Post a Comment
If you have any doubts, Please let me know
Thanks!