6

I'm just getting started in python, and I'm trying to test a user-entered string as a palindrome. My code is:

x=input('Please insert a word')
y=reversed(x)
if x==y:
    print('Is a palindrome')
else:
    print('Is not a palindrome')

This always returns false because y becomes something like <reversed object at 0x00E16EF0> instead of the reversed string. What am I being ignorant about? How would you go about coding this problem?

ide
  • 19,942
  • 5
  • 64
  • 106
Matthew Sainsbury
  • 1,470
  • 3
  • 18
  • 42
  • 2
    Just because a function is named like something you need, that doesn't mean you can use it without seeing it's definition first. Take a look at [this page](http://docs.python.org/library/functions.html#reversed). `reversed` returns an *iterator*, and I'm pretty sure that's not what you wanted. – darioo Mar 05 '11 at 08:10
  • how about if I used: `y=[x].reverse` ? – Matthew Sainsbury Mar 05 '11 at 08:14
  • That's really not what you want either for multiple reasons: `[x]` creates a single element list where the first element is `x`. Then when you reverse the list, you now get a list back instead of an iterator, so `y` is still not a string. – user470379 Mar 05 '11 at 08:23
  • @user470379 okay... :/ Perhaps this isn't the best way, but I just want to get a feel for the lang... how would I do it by treating `x` as a list of chars (ok, so single character strings), and reversing that list? – Matthew Sainsbury Mar 05 '11 at 08:30

7 Answers7

19

Try y = x[::-1]. This uses splicing to get the reverse of the string.

reversed(x) returns an iterator for looping over the characters in the string in reverse order, not a string you can directly compare to x.

Justin Ardini
  • 9,768
  • 2
  • 39
  • 46
8

reversed returns an iterator, which you can make into a string using the join method:

y = ''.join(reversed(x))
user470379
  • 4,879
  • 16
  • 21
2

For future reference, a lambda from the answers above for quick palindrome check:

isPali = lambda num: str(num) == str(num)[::-1]

example use:

isPali(9009) #returns True
mirandalol
  • 445
  • 1
  • 7
  • 16
0

Try this code.

def pal(name):
        sto_1 = []
        for i in name:
                sto_1.append(i)

        sto_2 = []
        for i in sto_1[::-1]:
                sto_2.append(i)

        for i in range(len(name)):
                if sto_1[i] == sto_2[i]:
                        return "".join(sto_1), "".join(sto_2)
                else:
                        return "no luck"

name = raw_input("Enter the word :")
print pal(name)
rickydj
  • 629
  • 5
  • 17
0
list(reverse( mystring )) == list( mystring )

or in the case of numbers

list(reverse( str(mystring) )) == list( str(mystring) )
Richard Hayes
  • 145
  • 1
  • 2
  • 8
0

Try this code:

def palindrome(string):
    i = 0 
    while i < len(string):
        if string[i] != string[(len(string) - 1) - i]:
            return False
        i += 1
    return True

print palindrome("hannah")

0

Try this code to find whether original & reverse are same or not:-

if a == a[::-1]: 

#this will reverse the given string, eventually will give you idea if the given string is palindrome or not.