how to check if the string is pallindrome or not in python??
kisan
0
Newbie Poster
Recommended Answers
Jump to PostThere is no built in function that I know of to check that.
On thing you might try doing is reversing the string and then comparing each element.
inString = 'racecar' reversestring = '' for i in range(0,len(inString)-1: reversestring.append(inString[i]
Another way to do it would be …
Jump to Postthere are a couple of posts regarding palindromes in the forum, have a look around.... but basically the algorithm is what mn_kthompson suggested :)
Jump to Postjust compare the inString with reversestring using the == operator
Jump to PostBut how could we test if the given string is pallindrome or not. I need a syntax that returns true if the string is pallindrome otherwise false.
>>> def is_palindrome(s): ... return s.lower() == s[::-1].lower() ... >>> is_palindrome('test') False >>> is_palindrome('racecar') True >>> is_palindrome('RACECAR') True
All 14 Replies
mn_kthompson
3
Junior Poster
masterofpuppets
kisan
0
Newbie Poster
abhi_elementx
1
Junior Poster
snippsat
661
Master Poster
mn_kthompson
commented:
Very elegant solution to palindrome problem
+2
abhi_elementx
1
Junior Poster
masterofpuppets
vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague
kisan
0
Newbie Poster
jlm699
320
Veteran Poster
vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague
mn_kthompson
3
Junior Poster
mn_kthompson
3
Junior Poster
Gribouillis
1,391
Programming Explorer
Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.