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)

# 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 = [

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"]

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"]

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)

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:

has_cyan = "cyan" in colors

Sort list of strings

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

numbers = ["one", "two", "three", "four"]

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:



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.

Improve your Python skills fast

The fastest way to learn programming is with lots of practice. Learn a programming concept, then write code to test your understanding and make it stick. Try our online interactive Python course today—it's free!

Learn more about the course

Want to get better at Python quickly? Try our interactive lessons today! Memberships are 100% FREE this week only!