Beginner Python Exercises

Reverse a String in Python

One slice does it: "CodeUtility"[::-1] gives "ytilitUedoC". See how the step value makes it work.

  • Beginner
  • 2 min read
  • 1 runnable example
What you'll learn
  • Slicing with a negative step
  • reversed() and "".join()
  • Why strings are not reversed in place
"Code"[::-1]
ewas [3]
dwas [2]
owas [1]
Cwas [0]
The slice reads characters from the last index to the first.

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.

From letter tiles to code
message🔤 The tiles in their original order
[::-1]⬅️ Start at the end and step backwards
reversed_message📝 A new row written in reverse
print(reversed_message)📣 Show the result
The original string stays the same; the slice creates a new one.
In one sentence

Slicing with [::-1] reads a sequence from its last item to its first and returns a new reversed copy.

Key points

  1. message[::-1] leaves start and stop empty, so the whole string is used.
  2. The step -1 walks from the last character to the first.
  3. reversed_message is a new string; message still holds "CodeUtility".
  4. 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

Run code →
main.py
message = "CodeUtility"
reversed_message = message[::-1]

print(reversed_message)
Get a Run button for this example

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.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in the Python compiler →

Common mistakes

Trying to reverse a string in place

Strings have no reverse() method because they cannot change.

Problem
message.reverse()
Fix
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.

All challenges