Python strings are immutable sequences, so the most direct approach is to create a reversed copy. The slice syntax reads any sequence from right to left when you use a negative step, and it’s the fastest way to reverse a string in typical cases. For user-visible text with emoji, flags, and combined characters, reverse grapheme clusters instead of raw code points to avoid corrupting symbols.
Method 1 — Reverse with slicing (idiomatic and fast)
This uses the standard slice form [start:stop:step]
, where a step of -1
walks the string from end to start. See Python’s sequence slicing in the docs for strings and sequences.
Step 1: Define the input string.
text = "Hello World"
Step 2: Create the reversed string with a reverse slice.
rev = text[::-1]
Step 3: Use or print the result.
print(rev) # dlroW olleH
Optional (many short strings): Precompute the reverse slice once and reuse it.
Step 1: Create a reusable reverse-slice object.
R = slice(None, None, -1)
Step 2: Apply it to strings repeatedly.
for s in ["abc", "racecar", "python"]:
print(s[R])
Method 2 — Use reversed()
with join()
(readable and flexible)
The built-in iterator reversed()
returns characters in reverse order, which you can join into a new string. See the official docs for reversed().
Step 1: Get a reverse iterator over the string.
it = reversed("Hello World")
Step 2: Join the characters into a string.
rev = "".join(it)
print(rev) # dlroW olleH
Method 3 — Reverse with a loop (explicit, easiest to trace)
This shows the mechanics but is slower because strings are immutable (each concatenation creates a new string).
Step 1: Initialize an accumulator.
rev = ""
Step 2: Prepend each character to build the reverse.
for ch in "Hello World":
rev = ch + rev
Step 3: Use or print the result.
print(rev) # dlroW olleH
Method 4 — Reverse grapheme clusters for correct emoji and accents (Unicode-safe)
Some visible “characters” are grapheme clusters made of multiple code points (for example, flags, skin-tone modifiers, and combining accents). Reversing raw code points can change meaning, such as turning the UK flag into BG. Unicode grapheme cluster boundaries are defined by UAX #29.
Step 1: Install a library that can iterate grapheme clusters.
pip install grapheme
Step 2: Import the library.
import grapheme
Step 3: Split the string into grapheme clusters.
clusters = list(grapheme.graphemes("👈🏾👆 🇬🇧 test"))
Step 4: Reverse the clusters and join them back into a string.
rev = "".join(clusters[::-1])
print(rev)
Tip: Compare results to see why this matters.
# Code point reversal (may be wrong for some emoji):
print("🇬🇧"[::-1]) # Often shows 🇧🇬
# Grapheme-aware reversal (preserves graphemes as units):
import grapheme
print("".join(list(grapheme.graphemes("🇬🇧"))[::-1])) # Stays 🇬🇧
When to use each method
- Use slicing for most strings; it’s idiomatic and fast.
- Use
reversed()
+join()
if you prefer iterators or need to plug into APIs that expect iterables. - Use grapheme-aware reversal for user-facing text containing emoji, flags, skin tones, or combining marks.
Pick slicing for performance and simplicity, and switch to grapheme-aware reversal when correctness for emoji and accents matters.
Member discussion