List of Strings in Python: How to Use Them in Your Programs — CODEFATHER

Claudio Sabato
6 min readNov 3, 2023

--

A list of strings is a very common data structure to use in your Python programs. With this tutorial, you will learn to create and manipulate lists of strings in Python.

The syntax to create a list of strings is the standard syntax to create any lists in Python, you will specify the strings in the list comma-separated within square brackets. You can use all the list methods to work on a list of strings either if you want to add an element to it, remove an element from it, or update an element.

Let’s start by creating a list of strings!

How to Create a List of Strings in Python

Open the Python shell and start creating a list of lists straight away.

First of all, specify the name of the variable for your list followed by the equal sign. Then on the right side of the equal sign, specify the list by doing the following:

  • Open a square bracket to start the list.
  • Specify the elements in the list of strings separated by commas. Specify every element within single or double quotes considering that every element of the list is a string.
  • Close the square bracket to end the list.
>>> animals = ['dog','cat','tiger','lion']
>>> type(animals)
<class 'list'>

As you can see from the code above, you can use the type() built-in function to confirm that the variable you have created is a list.

How to Access an Element in a Python List of Strings

An index is used in lists to access each element of the list, the value of the index starts from zero and the highest value is the number of elements in the list minus one.

To print the value of an element in a list of strings based on the index you can use the following syntax

print(list_name[index_value])

For instance, to print the first element of the list animals you can use the following Python code:

>>> print(animals[0])
dog

And to print the last element:

>>> print(animals[3])
lion

You can also use negative indexes to retrieve items starting from the end of the list of strings.

Let’s retrieve the last element of the list using the -1 index:

>>> print(animals[-1])
lion

Well done!

And what happens if you refer to an element in the list using an index that is bigger than the biggest index allowed for that list?

Let’s try it:

>>> print(animals[4])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range

You get a “list index out of range” error back…

It tells you that the list index you have used is out of the range of the indexes allowed in the animals list, whose biggest index is 3. That’s because the list of strings has 4 elements (and as explained before the index in a list starts from zero).

How to Print All the Strings in a List of Strings using Python

By passing a list to the print() function you can print the list with all its elements.

>>> print(animals)
['dog', 'cat', 'tiger', 'lion']

In a real program this is not very useful considering that instead of printing all the strings in a list including the enclosing square brackets, you will likely want to print each element in the list one by one.

To print the elements in a list of strings one at a time you can use a Python for loop:

>>> for animal in animals:
... print(animal)
...
dog
cat
tiger
lion

At each iteration of the for loop the variable animal contains one of the strings in the list of strings. This allows you to work with one element of the list at a time.

Imagine, for example, that you want to write a different logic in your code that executes different actions depending on the value of the string in the list of strings (in this case depending on each animal string).

Add a String to a List of Strings in Python

A common operation you will need to execute when using lists of strings in your Python programs is adding a new element to a list.

To add a new string at the end of a list of strings you can use the list append() method.

>>> animals = ['dog','cat','tiger','lion']
>>> animals.append('crocodile')
>>> print(animals)
['dog', 'cat', 'tiger', 'lion', 'crocodile']

When using the append() method we are specifying the list followed by a dot followed by the append() method. We then pass to the append() method the new string to be added to the list.

The dot notation you have used in this example is a common standard you will use when calling any list methods on a list variable.

Remove a String from a List of Strings

To remove a string from a list of strings use the remove() method that removes the first string with the value specified from the list.

>>> animals = ['dog','cat','tiger','lion']
>>> animals.remove('cat')
>>> print(animals)
['dog', 'tiger', 'lion']

If you want to remove a string at a specific index you can use the pop() method that takes the index of the string to remove.

>>> animals = ['dog','cat','tiger','lion']
>>> animals.pop(1)
'cat'
>>> print(animals)
['dog', 'tiger', 'lion']

You can see that the string removed from the list is also returned when you call the pop() method.

If you call the list pop() method without passing an index, the pop() method removes the last string from the list of strings.

>>> animals = ['dog','cat','tiger','lion']
>>> animals.pop()
'lion'
>>> print(animals)
['dog', 'cat', 'tiger']

Update a String in a List of Strings

A very common requirement when working with lists of strings is having to update the value of a specific string.

To update the value of a string in a list of strings simply assign a new value to the given element of the list by accessing it using its index.

>>> animals = ['dog','cat','tiger','lion']
>>> animals[1] = 'crocodile'
>>> print(animals)
['dog', 'crocodile', 'tiger', 'lion']

In the Python code above you have updated the value of the string at index 1 in the list from ‘cat’ to ‘crocodile’.

Get the Number of Elements in a List of Strings

When working with a list of strings you might want to get the number of elements in the list of strings. One reason could be that you want to display this information in a user interface for your users to see it.

To get the number of elements in a list of strings you can use Python’s len() built-in function.

>>> animals = ['dog','cat','tiger','lion']
>>> len(animals)
4

How to Join Two or More Lists of Strings in Python

If you have two or more lists of strings and you want to join them as part of your Python program, you can use the concatenation operator (+).

>>> reptiles = ['turtle', 'crocodile', 'iguana']
>>> felines = ['lion', 'tiger', 'jaguar']
>>> animals = reptiles + felines
>>> print(animals)
['turtle', 'crocodile', 'iguana', 'lion', 'tiger', 'jaguar']

Notice that the order of the elements in the final list depends on the order of the lists in the concatenation expression.

Let’s try to concatenate three lists of strings

>>> reptiles = ['turtle', 'crocodile', 'iguana']
>>> felines = ['lion', 'tiger', 'jaguar']
>>> birds = ['parrot', 'penguin', 'falcon']
>>> animals = birds + felines + reptiles
>>> print(animals)
['parrot', 'penguin', 'falcon', 'lion', 'tiger', 'jaguar', 'turtle', 'crocodile', 'iguana']

As you can see, this approach works for any number of lists of strings.

Conclusion

Now that you have completed this tutorial you have all the basic knowledge you need to start working with lists of strings in your Python program.

The great news is that you can use this knowledge to work with any type of list in Python, not just a list of strings.

Get familiar with the list methods we went through and make them your own by practicing them in your Python programs.

Also, let me know in the comments if you have any questions about this topic.

Related article: Do you know that Python also allows you to select a subset of elements from a list? Learn how to do that with the Codefather tutorial about Python list slicing.

Originally published at https://codefather.tech on November 3, 2023.

--

--

Claudio Sabato

Claudio Sabato is an IT expert with over 15 years of professional experience in Python/Bash programming, Linux Systems Administration and IT Systems Design.