Reverse a String in Python
Reverse a string with the extended slice [::-1], the idiomatic one-liner in Python.
Also known as: Python string reverse, reverse text Python
beginner
The slice s[::-1] steps backwards over the string, returning a reversed copy. It's the fastest and most idiomatic approach for a simple reversal.
What it is
Python strings are immutable sequences, so reversing produces a new string. The extended slice syntax s[start:stop:step] with a step of -1 and no bounds walks the string from end to start: s[::-1]. It's concise, fast, and works on any sequence including lists and tuples.
Alternatives exist — ''.join(reversed(s)) is more explicit and readable to newcomers, and both produce the same result. The slice is preferred in idiomatic Python for its brevity.
Worked example
s = "hello"
print(s[::-1]) # olleh
print("".join(reversed(s))) # olleh (equivalent, more explicit)
Failure mode — when it misleads
Reversing by code unit can split characters made of multiple code points — an emoji with a skin-tone modifier or a combining accent can be visually broken by a naive reverse. For user-facing text with such characters, reverse by grapheme cluster (e.g. via the regex library's \X) rather than by raw character.
Related entries
Sources & further reading
- Common Sequence Operations (Python docs) (article)