Skip main navigation

£199.99 £139.99 for one year of Unlimited learning. Offer ends on 28 February 2023 at 23:59 (UTC). T&Cs apply

Find out more

Strings as lists

Can we manipulate strings in similar ways to lists? Find out how in this article.

Although they may not look the same, in Python we can treat strings in a very similar way to lists. In Python terms, they are both “iterables”. This means that, in much the same way as we can use a for loop to repeat an action for each element in a list, we can do the same for each character in a string:

my_list = ['apple', 'banana', 'pear']
my_string = 'fig'

for item in my_list:

for item in my_string:

The code inside each of these loops will run three times, as the list has three elements, and the string has three characters.

We can also access characters in strings in the same way as elements in lists, using indices inside square brackets. For example, for the string topic = "algorithms", topic[0] would refer to the first character, which in this case is "a". topic[1] would give "l", topic[2] would give "g", and so on. As with lists, you can use negative numbers to count back from the end of the list: topic[-1] would give "s", while topic[-5] would give "i".

Slicing strings also works in the same way as slicing lists. Try out the following code:

topic = "slicing strings"


What strings are printed in each case?

Check that you are happy with how this works. The number before the colon gives the index of the first character in the slice, while the number after the colon gives the index of the first character which will not be included in the slice. Also, the space between the two words counts as a character, and in this string its location gives an index of 7.

Here is an example of a list of team names:


On scoreboards, sports teams are sometimes referred to using a contraction of their name, often the first three letters. Can you write some code in order to create a new list, teams_short, containing three-letter contractions of each of the teams in the list, in the same order as they appear in teams?

Share your code, problems, and suggestions in the comments.

This article is from the free online

Programming 102: Think Like a Computer Scientist

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education