9.2. Strings, Lists and Tuples

Throughout the first chapters of this book we have used strings to represent words or phrases that we wanted to print out. Our definition was simple: a string is simply some characters inside quotes. A string is like a list, but can hold different types of objects. In Chapter 5, when we introduced for loops, we introduced the index operator to access individual characters of a string while iterating over a string and we introduced lists of items that we can iterate over as well. In this chapter, we explore strings and lists in much more detail.

9.2.1. Strings

Strings can be defined as sequential collections of characters. This means that the individual characters that make up a string are in a particular order from left to right.

A string that contains no characters, often referred to as the empty string, is still considered to be a string. It is simply a sequence of zero characters and is represented by ‘’ or “” (two single or two double quotes with nothing in between).

9.2.2. Lists

A list is a sequential collection of Python data values, where each value is identified by an index. The values that make up a list are called its elements. Lists are similar to strings, which are ordered collections of characters, except that the elements of a list can have any type and for any one list, the items can be of different types.

There are several ways to create a new list. The simplest is to enclose the elements in square brackets ( [ and ]).

[10, 20, 30, 40]
["spam", "bungee", "swallow"]

The first example is a list of four integers. The second is a list of three strings. As we said above, the elements of a list don’t have to be the same type. The following list contains a string, a float, an integer, and another list.

["hello", 2.0, 5, [10, 20]]

Note

WP: Don’t Mix Types!

You’ll likely see us do this in the textbook to give you odd combinations, but when you create lists you should generally not mix types together. A list of just strings or just integers or just floats is generally easier to deal with.

9.2.3. Tuples

A tuple, like a list, is a sequence of items of any type. The printed representation of a tuple is a comma-separated sequence of values, enclosed in parentheses. In other words, the representation is just like lists, except with parentheses () instead of square brackets [].

One way to create a tuple is to write an expression, enclosed in parentheses, that consists of multiple other expressions, separated by commas.

julia = ("Julia", "Roberts", 1967, "Duplicity", 2009, "Actress", "Atlanta, Georgia")

The key difference between lists and tuples is that a tuple is immutable, meaning that its contents can’t be changed after the tuple is created. So, once a tuple is created, it’s not possible to add another value to it, to remove a value from it, or replace any of the values in the tuple.

To create a tuple with a single element (but you’re probably not likely to do that too often), you have to include a comma after the item, because without that comma, Python treats the item as a string, float or integer. For example, in the code below, Python treats (5) as an integer in parentheses, but treats (5,) as a tuple:

You might wonder why anyone would create a tuple - what’s the point of data that can’t be changed? Tuples are often used for exactly that purpose - to create data that can’t be accidentally tampered with. For really important information, it may be criticial to be able to verify that data was not edited after it was entered/created.

9.2.4. Index Operator: Accessing Elements of a String, List or Tuple

In Chapter 5, we introduced the index operator and how it is used to access elements of a string or list. We review this briefly here - the index operator applies to tuples in the same way.

We use the index operator ( []) to access an individual element of a string, list, or tuple. The expression inside the brackets specifies the index and must evaluate to an integer (the 3.75th element of a list doesn’t make any sense!). Remember that indices start at 0. Any integer expression can be used as an index and a negative index value will locate items from the right instead of from the left.

While we are taught as children to count from 1, in most programming languages we count/index from zero. This means that when we talk about the first item, we need to remember that is at index 0, and when we talk about the second item, it is at index 1. The nth character and the character AT INDEX n are different then: The nth character is at index n-1. Make sure you are clear on what you mean! If you find indexing from zero weird, it might help to think about the index as the offset from the first position in the list, tuple, or string. So if we declare a string variable like this: greeting = "hello", the letter ‘e’ is offset by 1 from the first character and we access the ‘e’ by using greeting[1].

9.2.5. Adding and Removing Items from Lists

A different way to build a list is to add items to it one at a time using the append method, like this:

You can also use append to add more items to an existing list. New items are added on to the end of the list:

You can remove items from a list using the remove() method, but you have to make sure the item is in the list before you remove it, otherwise you will get an error:

Note that if the item you want to remove is in the list multiple times, the remove method will only remove the first instance of the item:

There is one other useful method on lists: pop(). Pop removes an item from the end of a list (if you want that item, you need to capture it into a return variable). For example:

Of course, all of the methods discussed above only apply to lists, because only lists can be changed. You cannot call append() or remove(x) or pop() on a tuple.

Check your understanding

You have attempted of activities on this page