3

Possible Duplicate:
Reverse a string in Python

I understand that data in Python (strings) are stored like lists

Example:

string1 = "foo"
"foo"[1] = "o"

How would I use the list.reverse function to reverse the characters in a string? Such that I input "foo" and get returned back "oof"

1
  • Well, actually is more like a tuple, not string, I mean is inmutable. Commented Jul 2, 2011 at 20:56

3 Answers 3

14

You normally wouldn't.

>>> "foo"[::-1]
'oof'
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, good stuff. TIL extended slices!
3

Like this:

''.join(reversed(myString))

Comments

2

If you want to use list.reverse, then you have to do this:

c = list(string1)
c.reverse()
print ''.join(c)

But you are better using ''.join(reversed('foo')) or just 'foo'[::-1]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.