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
Advertisements
This entry was posted in Moving to Python 3. Bookmark the permalink.

12 Responses to How do I reverse a string in Python 3?

  1. Matt Chaput says:

    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.

  2. Even easier:

    >>> ”.join(reversed(‘abc’))
    ‘cba’

    • 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’

  3. loic jeannin says:

    Hi,

    >>> ‘abcdefgh'[::-1]
    ‘hgfedcba’

  4. Carl T. says:

    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):

    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.

  5. Emad says:

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

  6. Carl T. says:

    Emad,

    This link (in comments) suggests Stani’s Python Editor (he’s working on GNU Linux – Ubuntu) for Python 2.5:

    http://www.spencegreen.com/2008/12/19/python-arabic-unicode/

    Haven’t tried this myself.

  7. anjithkumar says:

    Yah This is very helpful to me thanx.

Comments are closed.