palindrome

Thread Solved

Join Date: Nov 2009
Posts: 4
Reputation: kisan is an unknown quantity at this point 
Solved Threads: 0
kisan kisan is offline Offline
Newbie Poster

palindrome

 
-1
  #1
19 Days Ago
how to check if the string is pallindrome or not in python??
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 128
Reputation: mn_kthompson is an unknown quantity at this point 
Solved Threads: 25
mn_kthompson mn_kthompson is offline Offline
Junior Poster
 
0
  #2
19 Days Ago
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.
  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 172
Reputation: masterofpuppets is an unknown quantity at this point 
Solved Threads: 40
masterofpuppets's Avatar
masterofpuppets masterofpuppets is offline Offline
Junior Poster
 
0
  #3
19 Days Ago
there are a couple of posts regarding palindromes in the forum, have a look around.... but basically the algorithm is what mn_kthompson suggested
My site ->> http://8masterofpuppets8.webs.com/
"My belief is stronger than your doubt."
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 4
Reputation: kisan is an unknown quantity at this point 
Solved Threads: 0
kisan kisan is offline Offline
Newbie Poster
 
0
  #4
19 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 113
Reputation: abhi_elementx is an unknown quantity at this point 
Solved Threads: 5
abhi_elementx's Avatar
abhi_elementx abhi_elementx is offline Offline
Junior Poster
 
0
  #5
19 Days Ago
just compare the inString with reversestring using the == operator
Last edited by abhi_elementx; 19 Days Ago at 6:04 am.
PEACE !
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 141
Reputation: snippsat is an unknown quantity at this point 
Solved Threads: 41
snippsat snippsat is offline Offline
Junior Poster
 
1
  #6
19 Days Ago
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.
  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 113
Reputation: abhi_elementx is an unknown quantity at this point 
Solved Threads: 5
abhi_elementx's Avatar
abhi_elementx abhi_elementx is offline Offline
Junior Poster
 
0
  #7
19 Days Ago
bravo!!! snipsat.
Dint know something like this exists in Python.
PEACE !
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 172
Reputation: masterofpuppets is an unknown quantity at this point 
Solved Threads: 40
masterofpuppets's Avatar
masterofpuppets masterofpuppets is offline Offline
Junior Poster
 
0
  #8
19 Days Ago
or you could do it using a loop:

  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; 19 Days Ago at 8:49 am.
My site ->> http://8masterofpuppets8.webs.com/
"My belief is stronger than your doubt."
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,942
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 911
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #9
18 Days Ago
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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 4
Reputation: kisan is an unknown quantity at this point 
Solved Threads: 0
kisan kisan is offline Offline
Newbie Poster

python more

 
0
  #10
17 Days Ago
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!"
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC