Reverse a String in Python
One slice does it: "CodeUtility"[::-1] gives "ytilitUedoC". See how the step value makes it work.
- Slicing with a negative step
- reversed() and "".join()
- Why strings are not reversed in place
A slice that walks backwards
The problem
Reversing text comes up in puzzles, in palindrome checks and when you process data written right to left. Python strings cannot be changed in place, so you need a way to build the reversed copy in one step.
Think of it like this
Reversing a string with a slice is like reading a row of letter tiles from the right end instead of the left. You do not move any tiles; you just start at the end and step backwards one tile at a time, writing down what you see.
message🔤 The tiles in their original order[::-1]⬅️ Start at the end and step backwardsreversed_message📝 A new row written in reverseprint(reversed_message)📣 Show the resultSlicing with [::-1] reads a sequence from its last item to its first and returns a new reversed copy.
Key points
message[::-1]leaves start and stop empty, so the whole string is used.- The step
-1walks from the last character to the first. reversed_messageis a new string;messagestill holds"CodeUtility".- The same slice works on lists and tuples.
When should you use it?
- Reversing text in puzzles and palindrome checks.
- Reading data that is stored in the opposite order, such as digits of a number.
- Reversing a list copy without changing the original.
When not to use it
- You need to change a list itself:
list.reverse()reverses it in place without making a copy. - You only need to loop backwards:
reversed()avoids building a new string.
Example code
message = "CodeUtility"
reversed_message = message[::-1]
print(reversed_message)
Result of running the code
ytilitUedoC
Code explanation
A slice has three parts, start:stop:step. Leaving start and stop empty means the whole string, and the step -1 means move backwards one character at a time, so Python starts at the last character and ends at the first. Because strings never change, the result is stored in a new variable and message keeps its original value.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.
Common mistakes
Trying to reverse a string in place
Strings have no reverse() method because they cannot change.
message.reverse()
reversed_message = message[::-1]
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Reverse a String Return the characters of a string in reverse order. Easy
- Palindrome String Check whether text reads the same forwards and backwards. Easy