944,214 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 2594
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 2nd, 2009
-1

palindrome

Expand Post »
how to check if the string is pallindrome or not in python??
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kisan is offline Offline
4 posts
since Nov 2009
Nov 2nd, 2009
0
Re: palindrome
There 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.
python Syntax (Toggle Plain Text)
  1. inString = 'racecar'
  2. reversestring = ''
  3. for i in range(0,len(inString)-1:
  4. reversestring.append(inString[i]

Another way to do it would be to compare the first element to the last element in your string and make sure they match the whole time. One thing that is helpful to remember when doing this is that string[-1] will give you the last element of the string, and string[-2] will give you the second to last. So if your string is racecar then inString[0] should equal inString[-1] and inString[1] should equal inString[-2] etc.
Reputation Points: 16
Solved Threads: 35
Junior Poster
mn_kthompson is offline Offline
148 posts
since Nov 2007
Nov 2nd, 2009
0
Re: palindrome
there are a couple of posts regarding palindromes in the forum, have a look around.... but basically the algorithm is what mn_kthompson suggested
Reputation Points: 20
Solved Threads: 74
Posting Whiz in Training
masterofpuppets is offline Offline
272 posts
since Jul 2009
Nov 3rd, 2009
0
Re: palindrome
Thank you very much!!
It was helpful. But 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kisan is offline Offline
4 posts
since Nov 2009
Nov 3rd, 2009
0
Re: palindrome
just compare the inString with reversestring using the == operator
Last edited by abhi_elementx; Nov 3rd, 2009 at 6:04 am.
Reputation Points: 11
Solved Threads: 7
Junior Poster
abhi_elementx is offline Offline
118 posts
since Dec 2007
Nov 3rd, 2009
1
Re: palindrome
Quote ...
But 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.
Python Syntax (Toggle Plain Text)
  1. >>> def is_palindrome(s):
  2. ... return s.lower() == s[::-1].lower()
  3. ...
  4. >>> is_palindrome('test')
  5. False
  6. >>> is_palindrome('racecar')
  7. True
  8. >>> is_palindrome('RACECAR')
  9. True
Reputation Points: 280
Solved Threads: 281
Master Poster
snippsat is offline Offline
773 posts
since Aug 2008
Nov 3rd, 2009
0
Re: palindrome
bravo!!! snipsat.
Dint know something like this exists in Python.
Reputation Points: 11
Solved Threads: 7
Junior Poster
abhi_elementx is offline Offline
118 posts
since Dec 2007
Nov 3rd, 2009
0
Re: palindrome
or you could do it using a loop:

Python Syntax (Toggle Plain Text)
  1. def isPalindrome( s ):
  2. s = s.lower()
  3. for i in range( len ( s ) / 2 ):
  4. if s[ i ] != s[ -1 -i ]:
  5. return False
  6. return True
  7.  
  8. >>> isPalindrome( "RACECAr" )
  9. True
  10. >>> isPalindrome( "test" )
  11. False
  12. >>> isPalindrome( "civiC" )
  13. True
  14. >>> isPalindrome( "ciniC" )
  15. True
  16. >>> isPalindrome( "cinac" )
  17. False
  18. >>>

Last edited by masterofpuppets; Nov 3rd, 2009 at 8:49 am.
Reputation Points: 20
Solved Threads: 74
Posting Whiz in Training
masterofpuppets is offline Offline
272 posts
since Jul 2009
Nov 3rd, 2009
0
Re: palindrome
Let's hope this is not homework and the OP had a chance to learn something.

In a true palindrome test you also need to remove everything but the case adjusted letters.
"Madam in Eden I'm Adam" is a palindrome.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 4th, 2009
0

python more

IT was quite helpful. so in case of palindromes how can we remove the symbols and capital letters using .upper and replace function?? for example
"Madam, in Eden I'm Adam!"
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kisan is offline Offline
4 posts
since Nov 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Back with more dumb questions!
Next Thread in Python Forum Timeline: Please help me get past this problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC