Use text[::-1] to reverse a string in Python by Unicode code point. For example, "planet"[::-1] produces "tenalp". The original string remains unchanged.
Another way to reverse Python strings is "".join(reversed(text)). Both approaches produce the same code-point order, including for empty input. If your text contains combining accents or multi-code-point emoji, however, a visible character may need to stay together as a group; the final section shows how to handle that case.
Reversing a string in Python with a slice
A slice has the form sequence[start:stop:step]. Omitting start and stop uses the appropriate ends of the sequence, while a step of -1 walks backward:
text = "planet"
backward = text[::-1]
print(backward) # tenalp
print(text) # planet
To reverse a string in Python examples, this slice is usually the shortest expression to read and write. The first two colons leave the bounds unspecified; the minus sign changes the direction. A step of -2 would visit every second code point in reverse order, which is a different operation.
Python strings are immutable: their contents cannot be changed in place. The slice produces a string value with the requested order rather than modifying text. Assigning the result back to the same name is fine, but it binds that name to the result; it does not edit the original string object.
text = "planet"
original = text
text = text[::-1]
print(text) # tenalp
print(original) # planet
This distinction matters when another part of a program holds a reference to the original value. Reassigning one variable does not silently reverse the value seen through the other variable.
Use reversed and join
The built-in reversed() returns an iterator that yields the string’s code points in reverse order. It does not itself return a string. Use the string method join() with an empty separator to assemble the yielded pieces:
text = "planet"
letters = reversed(text)
backward = "".join(letters)
print(backward) # tenalp
The empty string before .join() is the separator, so no spaces or punctuation are inserted. If you used "-".join(reversed(text)), the result would be t-e-n-a-l-p. That can be useful for display, but it is not the same reversal.
The two approaches to reversing a string in Python agree on their result for a given str value:
samples = ["planet", "a b", "", "123"]
for sample in samples:
assert sample[::-1] == "".join(reversed(sample))
The assertion checks equality of the resulting strings, not whether either method preserves every visible character. Both operate at code-point level. The Python sequence documentation describes slicing, and the built-in function reference describes reversed().
Empty input and other small cases
An empty string has no code points to visit, so both methods return "". A one-code-point string returns an equal string. Spaces, punctuation, and digits are reversed along with letters; neither method interprets words or numbers.
assert ""[::-1] == ""
assert "".join(reversed("")) == ""
assert "a b"[::-1] == "b a"
assert "123"[::-1] == "321"
When you reverse a string in Python code that accepts outside input, decide separately what to do with values that are not strings. For example, None is not an empty string and cannot be sliced this way. A function can reject it with a clear error, or the caller can convert input according to the application’s rules. Converting everything with str() also changes the meaning of the input, so do it deliberately.
A practical helper can keep the intended operation visible:
def reverse_code_points(text: str) -> str:
return text[::-1]
The type annotation documents the expected input; it does not enforce it at runtime. The function name also tells a future reader which unit is being reversed. A quick test with an empty string, ordinary text, and text containing an accent gives a more informative check than a single example.
Which approach should you choose?
When reversing a string in Python, the choice also depends on how the result will be used. If you need to compare the entire reversed value with another string, either completed expression is suitable. If you only need to inspect the first few code points from the end, an iterator lets you request them without first joining the whole sequence. Remember that an iterator is consumed as you advance it: create a new one if you need to traverse the same input again.
For a simple code-point reversal, the slice is concise and familiar to many Python readers. Use reversed() when an iterator fits the next operation, or combine it with join() when you want the sequence of steps to be explicit. In a code review, a brief function name or comment can explain why the direction matters.
To reverse a string in Python, both completed methods require a result containing the reversed text. The iterator returned by reversed() can be consumed piece by piece, but joining it constructs the result string. Choose clarity first; if performance matters for your workload, measure with representative input rather than assuming that one spelling is always faster.
Neither approach reverses word order while preserving the letters within each word. That is a separate task: split into words, reverse those elements, and join them with a chosen separator. Be aware that a simple whitespace split and join can change the original spacing.
Recognize and preserve visible characters
A Python str is a sequence of Unicode code points. One character a reader sees can contain more than one code point: an e followed by a combining acute accent is one common example. When you reverse a string in Python using either method above, the accent can move away from its base letter or attach to a different neighbor.
You can expose the issue without relying on how your terminal displays the result:
text = "Ae\u0301" # A, e, combining acute accent
print([f"U+{ord(c):04X}" for c in text])
# ['U+0041', 'U+0065', 'U+0301']
print([f"U+{ord(c):04X}" for c in text[::-1]])
# ['U+0301', 'U+0065', 'U+0041']
If the requirement is to reverse the visible units, segment the text into extended grapheme clusters first, reverse the clusters, and join them. The third-party regex package supports \X for this purpose:
import regex # Install separately: python -m pip install regex
text = "Ae\u0301"
clusters = regex.findall(r"\X", text)
result = "".join(reversed(clusters))
assert result == "e\u0301A"
The accent stays with e. Unicode’s text segmentation specification explains grapheme boundaries. Test combining marks and emoji from your actual input, and choose a library and Unicode version appropriate to your application. Normalizing text can help with particular representations, but normalization alone does not turn every emoji sequence or combining sequence into one code point.