Reversing a String in Python

The easiest and fastest way to reverse a string in Python is to use the slice operator [start:stop:step]. When you pass a step of -1 and omit the start and end values, the slice operator reverses the string. A more verbose, but readable (and slower) version of the string reversal method is a combination of the reversed() and string.join() methods. However, these methods do not work for strings containing special Unicode characters. To reverse such a Unicode string, you must use external libraries (see an example below). In this Python Reverse String example, we are reversing the string using the slice operator. Below, you can see more Python string reversal methods examples with detailed descriptions. Click Execute to run the Python Reverse String Example online and see the result.
Reversing a String in Python Execute
print('Hello World!'[::-1])
Updated: Viewed: 6367 times

What is a string in Python?

From a Python perspective, a string is an object consisting of an ordered sequence of characters. In Python 3, strings are stored as an array of 16-bit Unicode bytes, encoded in UTF-8 by default. When implementing your custom string-reversing method, you likely need to access the individual characters in the string. You can use square brackets "[]" to access string characters by index.

Python strings are immutable. This means that once created, the string can no longer be changed. All string manipulation methods return a copy of the string and do not modify the original. The built-in "str" library has an extensive set of methods for working with strings. It provides methods for searching, concatenating, reversing, splitting, and comparing strings.

Reverse the string using the Python slice operator

The slice operator [::] extracts a portion of a string from the provided string. But if you pass -1 as the last parameter, the slice operator will work in reverse order. If you do not pass the first two parameters (start and end positions), the slice operator will process the entire string. Thus, you can reverse the string using the [::-1] operator.

Reverse string using [::-1]
print('Python'[::-1])

# nohtyP

Python Slice Operator Syntax

Slice() returns a slice of the object for a specified sequence (string, tuple, list, range, or bytes). Slicing allows you to write clear, concise and readable code.

Python slice() Syntax
slice(start, stop, step)

Where:
  • start (optional): the starting integer from which to start slicing the object. The default is "None".
  • stop: is an integer to which slicing is performed. Slicing stops at index stop -1 (last item).
  • step (optional): an integer value specifying the increment between each index for the slice. The default is "None"

Reverse the string using Python reversed() and string.join() and methods

The Python reversed() method takes a sequence and returns an iterator that accesses the given sequence in reverse order. Since a string is a sequence of characters, we can use the reversed() method to get the string characters in reverse order.

Reverse string using reversed()
a = 'Python'
for symbol in reversed(a):
    print(symbol)

# n
# o
# h
# t
# y
# P

To get back a string from the resulting sequence, you can use the string.join() method. The string.join () method concatenates the elements of the given list into a string using the supplied string as a separator. When used together, reversed() and join() can reverse the string.

Reverse string using reversed() and string.join()
a = 'Python'
a2 = reversed(a)
result = "".join(a2)

print(result)

# nohtyP

Reverse the string using the Python for loop

Using Python for loop, you can iterate over characters in a string and append a character to the beginning of a new string. This will reverse the provided string.

Reverse string using for loop
a = 'Python'
b = ''
for c in a:
    b = c + b

print(b)

# nohtyP

Reverse the string using recursion

There are many ways to reverse a string using recursion. In the presented method, the recursive function copies the last character of the string to the beginning of a new string and recursively calls itself again, passing in the string without the last character.

Recursive string reversal method
def reverse_str(a: str) -> str:
    if not a:
        return ""
    return a[-1] + reverse_str(a[0:-1])

a = 'Python'
print(reverse_str(a))

# nohtyP

Conclusion

While Python doesn't have a built-in method to reverse the string, there are several ways to do it with just a few lines of code. You can use the slicing operator, the reversed() and string.join() methods, reverse the string using a for loop or recursion. Or, you can easily implement your own string-reversing method.

See Also