How to Reverse a String in Python

The fastest way to reverse a string in Python is slicing with a step of -1:

invertido = "hola"[::-1]

When do you need this?

Reversing a string is a classic exercise and a very useful trick in Python. You need it to check palindromes, process data that comes reversed, or simply to understand how slicing works. In this guide you will learn how to reverse a string in Python with slicing, reversed(), loops and recursion.

The shortest and most recommended way to reverse a string in Python is slicing with a step of -1.

texto = "hola"
invertido = texto[::-1]
print(invertido)
# aloh

Method 2: Reverse with reversed()

The built-in function reversed() returns the characters in reverse order. Combine it with join() to build the string.

texto = "python"
invertido = "".join(reversed(texto))
print(invertido)
# nohtyp

Method 3: Reverse with a for loop

You can also reverse the string manually. This helps you understand how indexing works in Python.

texto = "hola"
invertido = ""
for caracter in texto:
    invertido = caracter + invertido
print(invertido)
# aloh

Method 4: Reverse with recursion

A recursive solution is elegant and a great way to practice recursion. If the string is empty or has one character, it is already reversed.

def invertir(texto):
    if len(texto) <= 1:
        return texto
    return invertir(texto[1:]) + texto[0]

print(invertir("hola"))
# aloh

Method 5: Reverse the words of a phrase

If you want to reverse the order of the words instead of the characters, split the text and reverse the list.

frase = "hola mundo python"
invertida = " ".join(frase.split()[::-1])
print(invertida)
# python mundo hola

Method comparison

Method Advantage When to use it
Slicing [::-1] Fastest and shortest Always recommended
reversed() Readable When you want clarity
for loop Educational Understanding indexes
Recursion Elegant Practicing recursion
split()[::-1] Reverses words Phrases and sentences

Common errors and solutions

Trying to reverse a string in place

Strings are immutable in Python. You cannot modify them; you always create a new string with one of the methods above.

Forgetting join() with reversed()

reversed() returns characters one by one. Without "".join() you get an iterator, not a string.

Frequently asked questions

What is the fastest way to reverse a string in Python?

Slicing with [::-1] is the fastest and most readable option.

Does [::-1] work with any sequence?

Yes. Slicing works with strings, lists, tuples and any other sequence in Python.

Can I reverse a string in place?

No. Strings are immutable in Python, so you always create a new string.

how to reverse a string in python, reverse string python, invertir cadena python, python string slice, reversed python, python recursion, reverse words python

Save this tutorial and share it if it helped you. Find more practical guides in the Python how-to section.