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


What is an iterable, and how are they useful in programming? In this article, Martin O'Hanlon introduces an example of an iterable.
A photograph of two women coding at a laptop

You might be thinking …


That’s OK. An iterable is just something which can be “iterated”, or stepped through one bit at a time, such as a list of numbers or words. Before delving into the subject of iteration in Python, we will take some time to explore iterables and how they work.

Though you may not have realised it, you have already used an iterable during this course: a string.

Conceptually a string is a list of characters in an order: for example, “hello” is a list of 5 characters in the order “h” “e” “l” “l” “o”.

Understanding how lists and iterables work is important because this is how Python iterates: by running the same code for each item in a list.

First let’s explore the strings as iterables in the REPL to see how they work.

  • Open the REPL (or console in Trinket).

Mu with the REPL button circled in red, with the REPL open at the bottom of the screen, ready to take input, showing the prompt 'In [1]:'

A trinket with the down button to the right of the play symbol button highlighted by a red rectangle, and with the 'Console' option that has appeared below this also highlighted with a red rectangle

  • Create a string variable called phrase and assign it the value "hello", by typing the following into the REPL and pressing Enter.
phrase = "hello"

Mu with the REPL open, with 'phrase = "hello"' having been entered after the prompt 'In [1]: ' The cursor is now on the next line, after the prompt 'In [2}: ']

You can access individual elements (or items) in a list or split them up into parts by using indices, an index being a position in a list.

The string hello has 5 characters, the first element being h which has an index of 0, the second element e has an index of 1 and so on:

0 1 2 3 4
h e l l o

Tip: It’s a controversial subject, but the majority of computer programmers agree that indices should start at 0 and not 1, so it’s a good idea to embrace the idea that numbers start at zero!

You access elements in a list by using square brackets [] after the variable name, adding the index of the element you wish to get:

  • Get the first character from your string variable by entering this into the REPL.

Mu with the REPL open, with 'phrase = "hello"' having been entered after the prompt 'In [1]: '. On the next line, 'phrase[0]' has been entered after the prompt 'In [2]: '. The line after that reads "Out[2]: 'h' ". The cursor is now on the next line, after the prompt 'In [3]: '

Challenge: repeat the step above, changing the index to get the fourth character (“l”) in the string.

Indices don’t have to be positive numbers. You can also use negative indices to get elements from the end of the list. E.g. the last element in a list would have the index of -1:

-5 -4 -3 -2 -1
h e l l o
  • Get the last character from your string using the REPL.

What do you think the advantage is of using phrase[-1] over using phrase[4] to get the last element? Put your thoughts in the comments.

This article is from the free online

Programming 101: An Introduction to Python for Educators

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