Reverse Text in Python

Learn multiple ways to reverse strings in Python using slicing, reversed(), loops, and recursion. Understand Python's string handling and Unicode support.

Programming

Detailed Explanation

Reversing Strings in Python

Python provides elegant and concise ways to reverse strings, with built-in Unicode support that avoids many of the pitfalls found in other languages.

Method 1: Slice Notation (Most Pythonic)

reversed_str = original[::-1]

The [::-1] slice creates a reversed copy. This is the most idiomatic Python approach.

Method 2: reversed() + join()

reversed_str = "".join(reversed(original))

reversed() returns an iterator that yields characters in reverse order. join() concatenates them back into a string.

Method 3: For Loop

def reverse(s):
    result = ""
    for char in s:
        result = char + result
    return result

Method 4: Recursion

def reverse(s):
    if len(s) <= 1:
        return s
    return reverse(s[1:]) + s[0]

Warning: Python has a default recursion limit of 1000. This will fail for long strings.

Method 5: List Reverse

chars = list(original)
chars.reverse()
reversed_str = "".join(chars)

Unicode Handling

Python 3 strings are Unicode by default, so slicing works correctly with most international characters:

"こんにちは"[::-1]  # "はちにんこ"
"Hello 🌍"[::-1]   # "🌍 olleH"

Caveat: Combining characters (like accented letters composed of base + combining mark) may be reordered incorrectly. For full grapheme cluster support, use the grapheme library.

Performance

  • Slice notation is the fastest — it is implemented in C internally
  • reversed() + join() is slightly slower but equally correct
  • Recursion is the slowest and has stack depth limitations
  • For most use cases, [::-1] is the recommended approach

Use Case

Python string reversal is a core skill for Python developers, data scientists processing text data, and students preparing for coding challenges. Python's slice notation makes it one of the most concise reversal expressions in any programming language.

Try It — Reverse Text

Open full tool