I've created a python program where the user enters a word, then the program turns the word into a list. The user then enters a letter and then the program tell you how many times the letter appears. Now I need to add more program to check it the word that was entered at the beginning is a palindrome. If it is there will be a good message and if not a false message. Not really sure how to start. Any help?
-
1Possible duplicate of [Fast and pythonic way to find out if a string is a palindrome](http://stackoverflow.com/questions/34637002/fast-and-pythonic-way-to-find-out-if-a-string-is-a-palindrome) – Mar 01 '16 at 08:34
4 Answers
You can reverse the string and check whether it's equal to the input string:
def palindrome(s):
return s == s[::-1]

- 1
- 1

- 90,689
- 19
- 115
- 207
There is a known "trick" for that in python, reversing the word using [::-1]
. Not the most efficient though:
>>> "racecar" == "racecar"[::-1]
True
>>> "racecars" == "racecars"[::-1]
False

- 30,449
- 6
- 70
- 88
-
1I know you shouldn't normally use `[::-1]` because it creates an often unneccesary list, but why is this not the most efficient in this case? – L3viathan Mar 01 '16 at 08:31
-
1You just stated why. You could do it with O(1) computation-memory and O(n) cpu-time, that is more efficient. – Reut Sharabani Mar 01 '16 at 08:33
-
1Asymptotically you might be right, but I doubt that it is more than the additional overhead for the iterator version for any size of word you'd want to know if it's a palindrome of. I tried it vs `all(a==b for a,b in zip(r,reversed(r)))` (is there a better way?), and the `[::-1]` version is much faster. – L3viathan Mar 01 '16 at 08:39
-
1I also think it's a question of the word's length. I assume that big words would require more memory fiddling and that's where iterators would win. If you're on python 2 make sure you're not creating a list with `zip` (use `itertools.izip`). – Reut Sharabani Mar 01 '16 at 08:41
-
1Absolutely, at some point the iterators would win (especially in the negative case). My point was that they don't for any case that involves actual words. (And I'm using Python 3) – L3viathan Mar 01 '16 at 08:59
-
English words, or even human language, is a very specific domain. I would expect iterators to win all cases though (even if by a marginal difference), so I'm a bit surprised. Perhaps manual iteration of two "pointers" (start, end) is going to give faster results avoiding `zip`'s tuples. – Reut Sharabani Mar 01 '16 at 09:04
You could use a simple conditional statement. This should be much easier to understand for the beginner. For example:
text = "Rapport"
def palindrome(text)
if text[::-1] == text:
return True
else:
return False
I believe this should be pretty easy to understand. text[::-1] is the fastest approach. A much slower approach is using ''.join(reversed(list)), where the list is the list you split your text into. You can verify which approach you want to take by timing your code using timeit.
Although, timing the slicing of your code is considered a bad benchmark so time the whole code. Hope I explained well enough. Good luck!
Edit: your code is good but you need to remember that a function takes in an argument as a value, which is what you manipulate in the function itself.
Your final code should look something like this: text = StrVar()
def getPalindrome(word):
if word == word[::-1]:
return True
else :
return False
palindrome = getPalindrome(text)
Notice that the function's argument is different from the text itself, which I assigned a string variable to also show that you don't need to give it a value right away. Make your code elegant.

- 494
- 1
- 4
- 15
Okay so I've looked over all of your answers. Would this code be suitable or even a proper peice of code which isn't utter s**t?
def getPalindrome()
if word == word[::-1] :
print(True)
else :
print(False)
palindrome = getPalindrome()

- 23
- 4
-
Your function **prints** boolean value, whereas it should **return** it. – Łukasz Rogalski Mar 01 '16 at 15:35