How do I reverse a string in Python 3?

With the improved support for Unicode in Python3, more and more folks will be working with languages (Arabic, Hebrew, etc.) that read right-to-left rather than left-to-right. So more and more folks will have a need to reverse a string.

Unfortunately, Python doesn’t have a built-in function, nor do string objects have a built-in method, to do what they will want.  The obvious techniques don’t work. This:

        try:
            print(1)
            s = "a b c"
            s = reverse(s)
            print(s)
        except Exception as e:
            print(e)

        try:
            print(2)
            s = "a b c"
            s = reversed(s)
            print(s)
        except Exception as e:
            print(e)

        try:
            print(3)
            s = "a b c"
            s.reverse()
            print(s)
        except Exception as e:
            print(e)

        try:
            print(4)
            s = "a b c"
            s.reversed()
            print(s)
        except Exception as e:
            print(e)

produces this output

        
        1
        name 'reverse' is not defined
        2
        <reversed object at 0x00BAB5F0>
        3
        'str' object has no attribute 'reverse'
        4
        'str' object has no attribute 'reversed'

Fortunately, the solution is not too difficult. A little one-line function will do the trick.

I call the function “rev” rather than “reverse” on the chance that Python will eventually acquire its own builtin function named “reverse”.

        def rev(s): return s[::-1]

In a comment, Michael Watkins has noted another possible implementation of the “rev” function.

        def rev(s): ''.join(reversed(s))

Now

        try:
            print(5)
            s = "a b c"
            s = rev(s)
            print(s)
        except Exception as e:
            print(e)

produces

        5
        c b a

12 thoughts on “How do I reverse a string in Python 3?

  1. I believe this method will mangle some Unicode sequences. Why would working with RTL languages require reversing a string? Rendering the string to the screen as RTL is a function of the GUI.

    • PS: I wanted to point out reversed() because in your test app above you seemed to discount reversed() as useful since it returned an object, not the string you expected. Of course, it isn’t the only easier way to do what you want.

      Arguably even more idiomatic, you can use a slice:

      >>> ‘abc'[::-1]
      ‘cba’

  2. Stephen,

    This talk on test driven development from Pycon India (you may have already seen it) has an example on palindromes, and shows some of the complexity for a Tamil example (reversing string):

    http://www.slideshare.net/Siddhi/test-driven-development-with-python

    Thanks for the post. I very much enjoy this sort of thing. There isn’t too much out there on Python 3 yet (that I’ve found, at least). The Far East and Southeast Asia seem to be where there are developers actively using Python 3’s Unicode identifier capability. That’s a different topic, though.

    Carl T.

    Carl T.

  3. Whatever I try with easygui does not work.
    Does anyboy know of a GUI that is easy to use and supports RTL languages?

Comments are closed.