Python Principles

Lists of strings in Python

This post describes how to work with lists of strings in Python. Lists are one of the most common data structures in Python, and they are often used to hold strings.

Quick examples

First we'll give some quick examples:

# define a list of strings
>>> names = ["Eve", "Alice", "Bob"]

# print the list of strings
>>> print(names)
['Eve', 'Alice', 'Bob']

# loop over the names list, and
# print each string one at a time
>>> for name in names:
>>>     print(name)
Eve
Alice
Bob

# add a name to the list
>>> names.append("Charlie")

# check if the list contains a string
>>> if "Bob" in names:
>>>     print("Bob is here")
Bob is here

# add another string list to it
>>> more_names = ["Ivan", "Gerth"]
>>> names = names + more_names

# sort the list
>>> names.sort()

# join the strings in the list by a comma
>>> comma_separated = ", ".join(names)

>>> print(comma_separated)
Alice, Bob, Charlie, Eve, Gerth, Ivan

The next sections describe these operations on string lists in more detail.

Create list of strings

To create a list of strings, first use square brackets [ and ] to create a list. Then place the list items inside the brackets separated by commas. Remember that strings must be surrounded by quotes. Also remember to use = to store the list in a variable.

So we get something like this:

colors = ["red", "blue", "green"]

It is also allowed to put each string on a separate line:

animals = [
    "deer",
    "beaver",
    "cow"
]

Add strings to list

When you already have a list of strings and you want to add another string to it, you can use the append method:

colors = ["red", "blue", "green"]
colors.append("purple")

You can also create a new list with only one string in it and add it to the current list:

colors = colors + ["silver"]

To print a whole list of strings in one line, you can simply call the built-in print function, giving the list as an argument:

colors = ["red", "blue", "green"]
print(colors)

If you want to convert a list to a string, you can use the built-in function repr to make a string representation of the list:

products = ["shelf", "drawer"]
products_as_string = repr(products)
print(products_as_string)

Concatenate lists of strings

You can use the + operator to concatenate two lists of strings. For example:

colors1 = ["red", "blue"]
colors2 = ["purple", "silver"]
concatenated = colors1 + colors2

Check if string is in list

You can use the in keyword to check if a list contains a string. This gives you a boolean value: either True or False. You can store this value somewhere, or use it directly in an if statement:

colors = ["pink", "cyan"]
if "pink" in colors:
    print("yes!")

has_cyan = "cyan" in colors
print(has_cyan)

Sort list of strings

To sort a list of strings, you can use the sort method:

numbers = ["one", "two", "three", "four"]
numbers.sort()

You can also use the sorted() built-in function:

numbers = ["one", "two", "three", "four"]
numbers = sorted(numbers)

Join list of strings

To join a list of strings by another string, you need to call the join method on the string, giving your list as an argument. For example, if we have this list:

colors = ["red", "blue", "green"]

Then calling:

print(", ".join(colors))

Will output:

red, blue, green

And similarly, print("|".join(colors)) will output:

red|blue|green

Conclusion

Being able to manipulate lists of strings is crucial for Python programmers. We've described ways to create, modify, print, sort, and join lists of strings in Python.

Want to get better at Python?

Try our practical beginner lessons, or test your skills with our challenges.

View Lessons Try Challenges