String Slicing ([::-1])
The most Pythonic and efficient way to reverse a string in Python is using slicing with [::-1]. This method is concise, readable, and implemented in C, making it the fastest option.
text = "Hello, World!"
reversed_text = text[::-1]
print(reversed_text) # Output: !dlroW ,olleHThe [::-1] slice tells Python to traverse the string from end to start, stepping backward by 1.
Using reversed() and join()
This method is explicit and memory-efficient, returning a reverse iterator that is joined into a string.
text = "Hello, World!"
reversed_text = ''.join(reversed(text))
print(reversed_text) # Output: !dlroW ,olleHIt's ideal for readability and when you need to process characters in reverse order.
Using a For Loop
Iterate through the string and prepend each character to a new string. While easy to understand, it's inefficient for large strings due to string immutability, resulting in O(n²) time complexity.
text = "Hello, World!"
reversed_text = ""
for char in text:
reversed_text = char + reversed_text
print(reversed_text) # Output: !dlroW ,olleHAvoid this method in performance-critical code.
Using List Comprehension
A compact, one-line solution that builds a list of characters in reverse order, then joins them.
text = "Hello, World!"
reversed_text = ''.join([text[i] for i in range(len(text)-1, -1, -1)])
print(reversed_text) # Output: !dlroW ,olleHEfficient and readable, but slightly less intuitive than slicing.
Using Recursion
A functional approach where the function calls itself with the substring excluding the first character, then appends the first character at the end.
def reverse_string(s):
if len(s) <= 1:
return s
return reverse_string(s[1:]) + s[0]
text = "Hello, World!"
print(reverse_string(text)) # Output: !dlroW ,olleHGood for learning recursion, but not efficient for long strings due to function call overhead.
Using a Stack (LIFO)
Convert the string to a list (stack), then pop characters one by one to build the reversed string.
text = "Hello, World!"
stack = list(text)
reversed_text = ""
while stack:
reversed_text += stack.pop()
print(reversed_text) # Output: !dlroW ,olleHEfficient and intuitive for understanding stack behavior.
Performance Summary
Fastest:
[::-1](slicing)Second:
''.join(reversed())Slowest: For loop with
+concatenation (due to immutability)
For most use cases, use [::-1] — it’s the standard, fastest, and most readable method.
Using slicing:
>>> 'hello world'[::-1]
'dlrow olleh'
Slice notation takes the form [start:stop:step]. In this case, we omit the start and stop positions since we want the whole string. We also use step = -1, which means, "repeatedly step from right to left by 1 character".
Using slicing:
>>> 'hello world'[::-1]
'dlrow olleh'
Slice notation takes the form [start:stop:step]. In this case, we omit the start and stop positions since we want the whole string. We also use step = -1, which means, "repeatedly step from right to left by 1 character".
What is the best way of implementing a reverse function for strings?
My own experience with this question is academic. However, if you're a pro looking for the quick answer, use a slice that steps by -1:
>>> 'a string'[::-1]
'gnirts a'
or more readably (but slower due to the method name lookups and the fact that join forms a list when given an iterator), str.join:
>>> ''.join(reversed('a string'))
'gnirts a'
or for readability and reusability, put the slice in a function
def reversed_string(a_string):
return a_string[::-1]
and then:
>>> reversed_string('a_string')
'gnirts_a'
Longer explanation
If you're interested in the academic exposition, please keep reading.
There is no built-in reverse function in Python's str object.
Here is a couple of things about Python's strings you should know:
In Python, strings are immutable. Changing a string does not modify the string. It creates a new one.
Strings are sliceable. Slicing a string gives you a new string from one point in the string, backwards or forwards, to another point, by given increments. They take slice notation or a slice object in a subscript:
string[subscript]
The subscript creates a slice by including a colon within the braces:
string[start:stop:step]
To create a slice outside of the braces, you'll need to create a slice object:
slice_obj = slice(start, stop, step)
string[slice_obj]
A readable approach:
While ''.join(reversed('foo')) is readable, it requires calling a string method, str.join, on another called function, which can be rather relatively slow. Let's put this in a function - we'll come back to it:
def reverse_string_readable_answer(string):
return ''.join(reversed(string))
Most performant approach:
Much faster is using a reverse slice:
'foo'[::-1]
But how can we make this more readable and understandable to someone less familiar with slices or the intent of the original author? Let's create a slice object outside of the subscript notation, give it a descriptive name, and pass it to the subscript notation.
start = stop = None
step = -1
reverse_slice = slice(start, stop, step)
'foo'[reverse_slice]
Implement as Function
To actually implement this as a function, I think it is semantically clear enough to simply use a descriptive name:
def reversed_string(a_string):
return a_string[::-1]
And usage is simply:
reversed_string('foo')
What your teacher probably wants:
If you have an instructor, they probably want you to start with an empty string, and build up a new string from the old one. You can do this with pure syntax and literals using a while loop:
def reverse_a_string_slowly(a_string):
new_string = ''
index = len(a_string)
while index:
index -= 1 # index = index - 1
new_string += a_string[index] # new_string = new_string + character
return new_string
This is theoretically bad because, remember, strings are immutable - so every time where it looks like you're appending a character onto your new_string, it's theoretically creating a new string every time! However, CPython knows how to optimize this in certain cases, of which this trivial case is one.
Best Practice
Theoretically better is to collect your substrings in a list, and join them later:
def reverse_a_string_more_slowly(a_string):
new_strings = []
index = len(a_string)
while index:
index -= 1
new_strings.append(a_string[index])
return ''.join(new_strings)
However, as we will see in the timings below for CPython, this actually takes longer, because CPython can optimize the string concatenation.
Timings
Here are the timings:
>>> a_string = 'amanaplanacanalpanama' * 10
>>> min(timeit.repeat(lambda: reverse_string_readable_answer(a_string)))
10.38789987564087
>>> min(timeit.repeat(lambda: reversed_string(a_string)))
0.6622700691223145
>>> min(timeit.repeat(lambda: reverse_a_string_slowly(a_string)))
25.756799936294556
>>> min(timeit.repeat(lambda: reverse_a_string_more_slowly(a_string)))
38.73570013046265
CPython optimizes string concatenation, whereas other implementations may not:
... do not rely on CPython's efficient implementation of in-place string concatenation for statements in the form a += b or a = a + b . This optimization is fragile even in CPython (it only works for some types) and isn't present at all in implementations that don't use refcounting. In performance sensitive parts of the library, the ''.join() form should be used instead. This will ensure that concatenation occurs in linear time across various implementations.
For example:
txt = "Hello World"[::-1]
Isn't the splice syntax [start : stop: step]? And default of start and stop are the beginning and end of the string? So that would make the above start at the beginning, stop at the end, but step by -1. That feels like it would start at the beginning, then step backwards to...before the beginning of the string?
Sorry for the silly question, I just can't figure out why this syntax works the way it does.
Why does [::1] reverse a string in Python? : learnprogramming
What's the best way to reverse a string in Python?
Can someone explain this reverse string in Python?
You already have an explanation here, but you can run your short code through this nice visual debugger and see it happening step-by-step.
More on reddit.comAll the ways to reverse a string in python (and performance)
this thing is inane wankery, if you're bottlenecked on reversing strings your program has serious issues. Not only are µbench generally problematic, µbeching string reversal is close to epitomic pointlessness
it doesn't actually work for reversing strings: "ntavŕat́ivH" is not the reverse of "Hvítárvatn", yet it's what you get if you reverse the NFD form. And if you assert that NFC will save you, I have bad news:
>>> print(s)
👦🏻
>>> print(s[::-1])
🏻👦alternatively, with a "more normal" human script:
>>> print(s)
देवनागरी
>>> print(s[::-1])
ीरगानवेदplease note that "ी" is not correct at all, it's a diacritical vowel attached to "र" in the original word not an independent character, in fact in my browser at least it completely breaks selection (I can't select the combining vowel alone, it always selects the preceding double quote as well) (and on its own I believe it should be ई though I could be mistaken)
reversing strings is a completely pointless operation in the first place