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"

Community
  • 1
  • 1
Louis93
  • 3,843
  • 8
  • 48
  • 94

3 Answers3

14

You normally wouldn't.

>>> "foo"[::-1]
'oof'
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
3

Like this:

''.join(reversed(myString))
ninjagecko
  • 88,546
  • 24
  • 137
  • 145
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]