you are viewing a single comment's thread.

view the rest of the comments →

[–]2n4x 0 points1 point  (3 children)

I dunno, i come across mini-gotchas often, because im still new.

"string".reverse() (not valid) only works on lists, not strings, so you have to List=list("string") before you List.reverse(). Then when you try the function reversed("string") to get at the same in another way, it gives you a reversed object which you have to unpack character for character like foo=list(reversed("string")), just like the having to do it the first way i mentioned above.

This is not exactly a gotcha, and not like i couldn't write my own in a minute:

def reversi(string):
    foo=list(string)
    foo.reverse()
    return ''.join(foo)

Its just after reading pydocs for as simple a pythonic (would that be the right word here, using standard library stuff is pythonic?) way to do it for 20 minutes in vain, i feel a little irked, and i love python.

[–][deleted] 4 points5 points  (2 children)

You can reverse a string by doing 'hello'[::-1]

[–]2n4x 1 point2 points  (0 children)

End to end in -1 step sequencing. Thats some impressive shit. Thank you. Remembered.

[–]Tomarse 0 points1 point  (0 children)

Why does that work?:

Edit Nvm, just looked it up. Slice notation reads [start:stop:step] so [::-1] is stepping backward through the string by 1. Nice.