Oct. 15, 2012

Reversing lists and strings

This short post will show how to do a reverse loops in Python. The first example will show how to do that in a list a and the other example how to reverse a string. Just open up a Python interpreter and test it out. Create a list with some values in it, like this:
L1 = ["One", "two", "three", "four", "five"]

#To print the list as it is, simply do:
print L1

#To print a reverse list, do:
for i in L1[::-1]:
    print i
We can also reverse strings, like this:
string = "Hello World"

print ' '.join(reversed(string))

d l r o W   o l l e H

Share this article

Recommended Python Training – DataCamp

For Python training, our top recommendation is DataCamp.

Datacamp provides online interactive courses that combine interactive coding challenges with videos from top instructors in the field.

Datacamp has beginner to advanced Python training that programmers of all levels benefit from.


Download Our Free Guide To Learning Python

* indicates required

Read more about:
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, PythonForBeginners.com only recommend products or services that we try personally and believe will add value to our readers.