Skip to content

How to Append a String to a List in Python

How to Append a String to a List in Python

Python lists are a common data type you’ll use to store data. Because they’re mutable (meaning they can be changed) and heterogeneous (meaning they can store different types of data), Python lists are very popular. Being able to add items to a list is a common operation. In this tutorial, you’ll learn how to append a string to a list in Python.

You may have tried to add a string to Python and saw some unexpected results. Perhaps, this is why you’re here! Well, look no further – you’ll learn all you need to know to be able to append a string to a list.

By the end of this tutorial, you’ll have learned the following:

  • How to append a string to a list in Python using different methods, such as .append(), .extend(), and .insert() and the + operator
  • Why appending a string to a list can lead to some unexpected results and how to prevent this

How to Append a String to a List with Python with append

What is the best way to append a string to a list in Python?

The best way to append a string to a list in Python is to use the list .append() method. This is because it most clearly expresses the intentions of the code and works without errors.

To see how to use the .append() method to add a string to a Python list, check out the example below. We use a list containing two strings, though this would work with other types of objects in the list as well.

# Append a String to a List in Python Using .append()
values = ['welcome', 'to']
values.append('datagy')

print(values)

# Returns:
# ['welcome', 'to', 'datagy']

In the example above, we used a simple use case to see how to add a string to a list. By passing a string into the .append() method, we were able to easily append the string.

Other List Methods to Append a String to a List

Python provides many different methods to append items to a list. In this section, we’ll explore three different methods that allow you to add a string to the end of a Python list:

  1. Python list.extend()
  2. Python list.insert()
  3. Python + operator

Let’s dive in!

How to Append a String to a List with Python with extend

The Python list.extend() method is used to add items from an iterable object to the end of a list. Because of this, we need to be careful, since Python strings themselves are iterable. Let’s see what happens when we try to add a string to a list using the .extend() method:

# Using .extend() to Append a String to a List
values = ['welcome', 'to']
values.extend('datagy')

print(values)

# Returns:
# ['welcome', 'to', 'd', 'a', 't', 'a', 'g', 'y']

We can see that this led to some unexpected results! Python unpacks the items in the string (since it’s iterable) and appends each item to the list. In order to prevent this behavior, we need to wrap the string in a list first:

# Using .extend() to Append a String to a List
values = ['welcome', 'to']
values.extend(['datagy'])

print(values)

# Returns:
# ['welcome', 'to', 'datagy']

We can see that by first wrapping the string in a list, that the issue was resolved. Keep in mind, however, that this is different than passing the string into the list() function, which would actually result in the same, unwanted result.

In the next section, you’ll learn how to use the .insert() method to append a string to a list.

How to Append a String to a List with Python with insert

The .insert() method allows you to pass an item into a list at a specific position. In order to append an item, we simply need to pass the string into the .insert() method at the last position.

# Using the .insert() Method to Append a String to a List
values = ['welcome', 'to']
values.insert(len(values), 'datagy')

print(values)

# Returns:
# ['welcome', 'to', 'datagy']

In the code block above, we use the .insert() method to append a string. Because we want to append the string, we pass in the position equal to the length of the list using the len() function.

How to Append a String to a List with Python with the + operator

Finally, let’s take a look at how we can use the + operator to add a string to the end of a list. Similar to the .extend() method, we need to add wrap the string in a list. If we don’t do this, each character in the string is appended individually. Let’s take a look at how this can be done:

# Using the + Operator to Append a String to a List
values = ['welcome', 'to']
values += ['datagy']

print(values)

# Returns:
# ['welcome', 'to', 'datagy']

In the example above, we used the augmented assignment operator to simplify our code. In the following section, you’ll learn how to append a string to an empty list.

How to Append a String to an Empty List with Python

Appending a string to an empty list in Python works in the same way as with any other list. You can use any of the methods that we explored above. However, as mentioned the .append() method is the clearest way of accomplishing this.

# Append a String to an Empty List
values = []
values.append('datagy')

print(values)

# Returns:
# ['datagy']

The .append() method is incredibly clear in terms of what it’s doing. Because of this, it’s the method I recommend using.

How to Append a String to the Beginning a List with Python

Python provides a number of different ways to prepend an item to a list. The simplest way, in my opinion, is to use the .insert() method. While in the previous sections you learned how to add an item to the end of the list, it’s even simpler to add an item to the beginning of a list.

Let’s take a look at how to do this:

# Add a String to the Beginning of a List
values = ['welcome', 'to']
values.insert(0, 'datagy')

print(values)

# Returns:
# ['datagy', 'welcome', 'to']

In the code block above, we used the .insert() method to add an item at the 0 index position. Because Python lists are 0-based indexed, this represents the beginning of the list.

Conclusion

In this tutorial, you learned many different ways to append a string to a list in Python. Being able to work with lists and strings is an essential skill for any Python developer. You first learned how to use different methods to add a string to a list. While Python provides many different options, the .append() method is by far the clearest method.

Then, you learned how to append a string to an empty list. Finally, you learned how to prepend a string to a Python list, at the beginning of the list.

Additional Resources

To learn more about related topics, check out the tutorials below:

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

Leave a Reply

Your email address will not be published. Required fields are marked *