Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 47

Reverse a String or List in Python

In this lesson: Reverse strings and lists in Python using slicing [::-1], reversed() and loops — examples with output. In Hindi & English.

Reversing in Python

Reversing a string or a list is a very common task. Python makes it easy — the shortest way is slicing with [::-1], but a loop helps you understand what actually happens. We will cover both, plus reversing a list in place.

💡 The quick way: text[::-1] reverses a string or list in one line.

The slicing way [::-1]

text = "Python"
print(text[::-1])
nohtyP

A slice is [start:stop:step]. Leaving start and stop empty means "the whole string", and a step of -1 walks backwards — so it comes out reversed. This is the most Pythonic way.

Reversing with a loop

To see how reversing works, build a new string character by character.

text = "Python"
rev = ""
for ch in text:
    rev = ch + rev      # add each char in front
print(rev)
nohtyP

The key is rev = ch + rev — each new character is added in front, so the string builds up backwards.

Dry run of the loop

Dry Run — "Python" reverse: rev = ch + rev
ch (each character)rev = ch + revrev now
P"P" + """P"
y"y" + "P""yP"
t"t" + "yP""tyP"
h"h" + "tyP""htyP"
o"o" + "htyP""ohtyP"
n"n" + "ohtyP""nohtyP"
💡 Understand it this way

Each character is added in front of rev (ch + rev, not rev + ch). So the newest character is always at the start, and the string builds up reversed. "Python" → "nohtyP". (Or simply: text[::-1].)

Reversing a list

nums = [1, 2, 3, 4]
print(nums[::-1])     # new reversed list
nums.reverse()        # reverse in place
print(nums)
[4, 3, 2, 1]
[4, 3, 2, 1]

nums[::-1] gives a new reversed list and leaves the original alone. nums.reverse() reverses the list itself and returns nothing — pick based on whether you want to keep the original.

Common mistakes

  • Writing rev + ch in the loop. That keeps the order; use ch + rev to reverse.
  • Expecting reverse() to return a value. It reverses in place and returns None.
  • Using reverse() on a string. Strings have no reverse() method — use slicing.
  • Forgetting the two colons. It is [::-1], not [:-1] (which just drops the last item).

Summary

  • The quickest reverse is slicing: text[::-1] for strings and lists.
  • A loop with rev = ch + rev shows how reversing works.
  • list.reverse() reverses in place; [::-1] makes a new copy.
  • Strings have no reverse() method — use slicing or reversed().
  • Reverse a number with int(str(n)[::-1]).

Next, use this in the palindrome program, or explore list comprehension.

Frequently Asked Questions

What is the easiest way to reverse a string in Python?
The easiest way is slicing: text[::-1]. The [::-1] means "take the whole thing, stepping backwards", which returns the string reversed in one short line.
How does the slice [::-1] work?
A slice is [start:stop:step]. Leaving start and stop empty means the whole sequence, and a step of -1 moves from the end to the start — so it reverses the string or list.
What is the difference between reversed() and [::-1]?
[::-1] returns a new reversed string or list directly. reversed() returns an iterator you loop over or pass to join/list. Both reverse; slicing is shorter, reversed() can be more memory-friendly for large data.
How do I reverse a list in place?
Use the list method my_list.reverse(), which reverses the list itself and returns nothing. If you want a new reversed list instead, use my_list[::-1] and keep the original unchanged.
Can I reverse a number in Python?
Not directly, but you can turn it into a string, reverse that, and convert back: int(str(n)[::-1]). This is a common trick for problems like checking palindromic numbers.
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicList vs Tuple vs Set vs Dict Your progress: 47 / 66 (71%) · Lesson 48 / 66 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free