Python Forum

Full Version: reversing strings
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hey this question isn't included in my homework but python is a part of my class so i hope it falls under this context but:
on my laptop i use pthon version 3 and ive been doing extra exercises on the topic and ive come across a problem that i dont quite understand and that involves reversing a string as a list to see if its writen the same backwards, this is what i came up with:

def palindrome():
    a=[input("palindrome? : "))
    print(a[::-1])    #just to see if a[::-1] actually reverses "a" (for some reason it doesn't)
    if a == a[::-1]:
        print("yes")
    else:
        print("no")
any solutions to why [::-1] doesn't work, im not really looking for other ways to reverse a string just why it doesnt work this way... thanks in advance ^^

westbob
check line 2; you have mix of [ and () brackets
sorry, that was a typo, i still get the same answer (for EX that a=["boat"] a[::-1] gives ['boat'])

ah! i see i shouldn't be using brackets and instead using (), got it thnks! ^^
Ok so what's your actual code? If you have a = [input("something")], then a isn't a string, it's a list with a single element. And reversing that list would be the same as doing nothing to it, since it's only got one element.
show us your actual code
>>> spam = 'boat'
>>> spam[::-1]
'taob'