Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reversing strings
#1
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
Reply
#2
check line 2; you have mix of [ and () brackets
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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! ^^
Reply
#4
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.
Reply
#5
show us your actual code
>>> spam = 'boat'
>>> spam[::-1]
'taob'
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  reversing a string kerzol81 3 2,794 Jul-18-2018, 09:24 PM
Last Post: perfringo
  Reversing a String grkiran2011 1 2,327 Dec-02-2017, 04:43 PM
Last Post: Larz60+
  Strings inside other strings - substrings OmarSinno 2 3,646 Oct-06-2017, 09:58 AM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020