Iterate over a string backwards in Python

avatar

Borislav Hadzhiev

Last updated: Aug 29, 2022

banner

Photo from Unsplash

Iterate over a string backwards in Python #

Use the reversed() function to iterate over a string in reverse order, e.g. for char in reversed(my_str):. The reversed() function takes an iterator, such as a string, reverses it and returns the result.

main.py
my_str = 'abcd' # ✅ iterate over string in reverse order (reversed()) for char in reversed(my_str): print(char) # 👉️ d, c, b, a # --------------------------------- # ✅ iterate over string in reverse order (negative slicing) for char in my_str[::-1]: print(char) # 👉️ d, c, b, a # --------------------------------- # ✅ iterate over string in reverse order (with index) for index, char in reversed(list(enumerate(my_str))): print(index, char) # 👉️ 3 d, 2 c, 1 b, 0 a

The first example uses the reversed() function to reverse a string.

The reversed function takes an iterator, reverses it and returns the result.

main.py
my_str = 'abcd' for char in reversed(my_str): print(char) # 👉️ d, c, b, a

Alternatively, you can use a negative step.

Iterate over a string backwards using a negative step #

Use a negative step to iterate over a string backwards, e.g. for char in my_str[::-1]:. The slice with a step of -1 is used to reverse the string before iterating over it with a for loop.

main.py
my_str = 'abcd' for char in my_str[::-1]: print(char) # 👉️ d, c, b, a

We used a negative step to reverse the string before iterating over it.

main.py
my_str = 'abcd' reversed_str = my_str[::-1] print(reversed_str) # 👉️ dcba

The syntax for string slicing is my_string[start:stop:step].

We omitted the start and stop values and only specified a negative step to reverse the string.

Which approach you pick is a matter of personal preference. I'd go with the reversed() function as I find it more readable and intuitive.

Iterate over a string backwards with index #

To iterate over a string backwards with index:

  1. Use the enumerate() function to get access to the index.
  2. Convert the enumerate object to a list and reverse the list.
  3. Use a for loop to iterate over the string in reverse order.
main.py
my_str = 'abcd' for index, char in reversed(list(enumerate(my_str))): print(index, char) # 👉️ 3 d, 2 c, 1 b, 0 a

We used the enumerate function to get access to the index when iterating.

The enumerate function takes an iterable and returns an enumerate object containing tuples where the first element is the index and the second is the item.

main.py
my_str = 'abcd' for index, char in enumerate(my_str): print(index, char) # 👉️ 0 a, 1 b, 2 c, 3 d

We used the list() class to convert the enumerate object to a list because the first argument the reversed() function takes is a sequence.

main.py
my_str = 'abcd' # 👇️ [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')] print(list(enumerate(my_str))) # 👇️ [(3, 'd'), (2, 'c'), (1, 'b'), (0, 'a')] print(list(reversed(list(enumerate(my_str)))))

The last step is to use a for loop to iterate over the list of tuples.

The first element in each tuple is the index and the second is the corresponding character.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.