Reverse a string in Python
Reverse a string with slicing or reversed, and understand why Unicode code-point reversal may split combining marks or emoji sequences.
Use text[::-1] to return a string in reverse order. The negative slice step walks through the string from its end toward its beginning.
Reverse with a slice
original = "hello"
backward = original[::-1]
print(backward) # olleh
print(original) # hello
Strings are immutable, so the original is unchanged. The slice form also works with other sequence types, including lists and tuples.
Use a reverse iterator
backward = "".join(reversed("hello"))
print(backward) # olleh
reversed() returns an iterator; join() assembles the yielded characters into a string. Slicing is usually the clearest choice for this simple task. Avoid assuming a fixed speed advantage without measuring your workload. Repeated string concatenation can copy growing intermediate strings and is a poor general strategy for building a reversed result.
Account for Unicode text
Python strings contain Unicode code points. A visible character can consist of several code points, such as a letter plus a combining accent or an emoji sequence. Code-point reversal can split those sequences:
text = "e\u0301" # Letter e followed by a combining acute accent
print(repr(text[::-1]))
If you need to preserve user-perceived characters, segment the text into grapheme clusters, reverse those clusters, and join them. A Unicode-aware library such as grapheme can provide that operation, but check which Unicode version it supports for your inputs. Reversing clusters also does not solve every bidirectional text-layout requirement.
FAQ
How do I reverse a list? items[::-1] returns a new list. items.reverse() mutates the list in place and returns None. reversed(items) yields items without constructing a reversed list itself.
Does reversing twice restore the string? Code-point slicing does: text[::-1][::-1] == text. That does not mean the intermediate reversed text displays correctly.
How do I reverse words instead? Use " ".join(text.split()[::-1]). This reverses whitespace-separated tokens but normalizes whitespace to single spaces; punctuation stays attached to its token.
References
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan