Reversing a Python String With the
[::-1] Slicing Trick
Strings follow the sequence protocol in Python. And all sequences support an interesting feature called slicing. You can view slicing as an extension of the square-brackets indexing syntax.
It includes a special case where slicing a sequence with
[::-1]produces a reversed copy. Because Python strings are sequences this is a quick and easy way to get a reversed copy of a string:
>>> 'TURBO'[::-1] 'OBRUT'
Of course, you can wrap this (slightly unwieldy) slicing expression into a named function to make it more obvious what the code does:
def reverse_string1(s): """Return a reversed copy of `s`""" return s[::-1]
So, how do you like this solution?
It’s short and sweet—but, in my mind, the biggest downside to reversing a string with the slicing syntax is that it uses an advanced Python feature that some developers would say is “arcane.”
I don’t blame them—list slicing can be difficult to understand the first time you encounter its quirky and terse syntax.
When I’m reading Python code that makes use of slicing I often have to slow down and concentrate to “mentally parse” the statement, to make sure I understand what’s going on.
My biggest gripe here is that the “
[::-1]” slicing syntax doesn’t communicate clearly enough that it creates a reversed copy of the original string.
For this reason I feel like using Python’s slicing feature to reverse a string is a decent solution, but it can be a difficult to read to the uninitiated.
Reversing a Python String Using
Reversing a string using reverse iteration with the built-in
reversed() is another option. You get a reverse iterator you can use to cycle through the elements in the string in reverse order:
>>> for elem in reversed('TURBO'): ... print(elem) O B R U T
reversed() does not modify the original string (which wouldn’t work anyway as strings are immutable in Python.) What happens is that you get a “view” into the existing string you can use to look at all the elements in reverse order.
This is a powerful technique that takes advantage of Python’s iterator protocol.
So far, all you saw was how to iterate over the characters of a string in reversed order. But how can you use this technique to create a reversed copy of a Python string with the function
>>> ''.join(reversed('TURBO')) 'OBRUT'
This code snippet used the method
.join() to merge all of the characters resulting from the reversed iteration into a new string. Pretty neat, eh?
Of course, you can once again extract this code into a separate function to create a proper “reverse string” function in Python. Here’s how:
def reverse_string2(s): """Return a reversed copy of `s`""" return "".join(reversed(s))
I really like this reverse iterator approach for reversing strings in Python.
It communicates clearly what is going on, and even someone new to the language would intuitively understand that I’m creating a reversed copy of the original string.
And while understanding how iterators work at a deeper level is helpful, it’s not absolutely necessary to use this technique.
One more approach you should check out:
The “Classic” In-Place String Reversal Algorithm Ported to Python
This is the “classic” textbook in-place string reversal algorithm, ported to Python. Because Python strings are immutable, you first need to convert the input string into a mutable list of characters, so you can perform the in-place character swap:
def reverse_string3(s): """Return a reversed copy of `s`""" chars = list(s) for i in range(len(s) // 2): tmp = chars[i] chars[i] = chars[len(s) - i - 1] chars[len(s) - i - 1] = tmp return ''.join(chars)
As you can tell, this solution is quite unpythonic and not very idiomatic at all. It doesn’t play to Python’s strengths and it’s basically a straight port of a C algorithm.
And if that wasn’t enough—it’s also the slowest solution, as you’ll see in the next section where I’ll do some benchmarking on these three implementations.
This video is about reversing a string in Python. Just for an instance if the string is qwerty then it would result in ytrewq.