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.
text[::-1] reverses a string or list in one line.The slicing way [::-1]
text = "Python"
print(text[::-1])
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)
The key is rev = ch + rev — each new character is added in front, so the string builds up backwards.
Dry run of the loop
| ch (each character) | rev = ch + rev | rev now |
|---|---|---|
| P | "P" + "" | "P" |
| y | "y" + "P" | "yP" |
| t | "t" + "yP" | "tyP" |
| h | "h" + "tyP" | "htyP" |
| o | "o" + "htyP" | "ohtyP" |
| n | "n" + "ohtyP" | "nohtyP" |
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]
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 + chin the loop. That keeps the order; usech + revto reverse. - Expecting
reverse()to return a value. It reverses in place and returns None. - Using
reverse()on a string. Strings have noreverse()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 + revshows 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?
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?
[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?
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?
int(str(n)[::-1]). This is a common trick for problems like checking palindromic numbers.