- Get link
- X
- Other Apps
Posted by
Naveen M K
on
- Get link
- X
- Other Apps
What is a Palindrome?
A palindrome is a word, number, phrase, or other sequences of characters which reads the same backward as forward, such as ‘’taco cat’’ or madam or racecar or the number 10801. Sentence-length palindromes may be written when allowances are made for adjustments to capital letters, punctuation, and word dividers, such as "A man, a plan, a canal, Panama!", "Was it a car or a cat I saw?" or "No 'x' in Nixon".
Coding using loops(for loop)
#To check whether a string is a palindrome or not.
s=input('Enter a word')
l=len(s)
mid=l//2
rev=-1
for a in range(l):
if s[a]==s[rev]:
rev-=1
else:
print("not a palindrome")
break
else:
print(s,'Is a palindrome')
Coding using slices
#To check whether a string is a palindrome or not using slices
s=input('Enter a word')
rev=s[::-1]
if s==rev:
print(s,'Is a palindrome')
else:
print("not a palindrome")


Comments
Post a Comment