Reverse a String in Python

Rate this post

There are four main ways to reverse a string in Python:

  • Slicing s[::-1] with negative step size traverses the string from right to left.
  • ''.join(reversed(s)) first creates an iterable of characters in reversed order, and then joins these characters to obtain the reversed string.
  • A for loop using the range(len(s)-1, -1, -1) function traverses the indices from the last to the first. In the loop body, you can then add the respective elements in reverse order.
  • A recursive function that takes one string as an element and returns the concatenation of the last character and the result of the function calling itself on the remaining characters except the last.

Here’s the most concise way to reverse a string as a graphic:

Reverse a String in Python

Reverse a String in Python Using Slicing

Slicing is a Python concept to carve out a substring from a given string s by using the square bracket notation s[start:stop:step] specifying the start index, the stop index, and the step size. You can set default start and stop indices and use negative step size -1 to reverse a given string in Python. The respective code would be s[::-1] to reverse string s.

Here’s an example that reverses a string using slicing:

s = '!dlrow olleh'
res = s[::-1]

# 'hello world!'

Reverse a String in Python Using For Loop

The traditional way to reverse a string is to use a for loop to successively add the last character of the remaining string to an initially empty string variable. As soon as you reach the last string character, and you’ve kept adding the last element to the string, you get the original string in reversed order.

This is exemplified in the following example:

s = '!dlrow olleh'
res = ''

for i in range(len(s)-1, -1, -1):
    res += s[i]
# hello world!

Reverse a String in Python Using Function reverse()

Python’s built-in reverse() function creates a new iterable with elements in reversed order. However, the iterable is not yet a string—to fully reverse the original string, you need to combine the elements in the iterable in order using the built-in string.join() method.

An example is given next:

s = '!dlrow olleh'
res = ''.join(reversed(s))

# hello world!

Reverse a String in Python Using Recursion

To reverse a string using recursion, you can define a function that calls itself.

  • The recursion base case is if the function is called on the empty string in which case, the string is its own reverse string.
  • The recursion non-base case is if the string is non-empty—now you can concatenate the result of calling the function on all characters except the last and the first character and return the result.

The function will then recursively call itself on shorter and shorter strings until the recursion base case is reached:

def recursive_reverse(s):
    if s:
        return recursive_reverse(s[1:]) + s[0]
    return ''

s = '!dlrow olleh'
# hello world!

Reverse a String in Python in One Line

The shortest one-liner solution to reverse a string in a single line of Python code is to use slicing with negative step size -1. For example, to reverse string '!dlrow olleh', you can suffix the square bracket notation [::-1] that goes from right to left and returns the reversed string.

Here’s an example:

print('!dlrow olleh'[::-1])