Add Character String to List in Python (4 Examples)

 

This short tutorial will show you how to join a character string to a list in Python.

Here, you can take a quick look at the tutorial structure:

Let’s dive into it!

 

Create Sample List of Strings

First of all, we will create a sample list of strings, to which we will add a character string.

my_list = ['apple', 'banana', ' grapes', 'cherry', 'watermelon']
print(my_list)
# ['apple', 'banana', ' grapes', 'cherry', 'watermelon']

The previous output shows that my_list is a list object that contains five strings. Let’s see how to add another string to my_list!

 

Example 1: Join Character String to List via append() Method

The append() method can be handy when adding a new element to a list. Consider the Python code below.

my_list.append("strawberry")
print(my_list)
# ['apple', 'banana', ' grapes', 'cherry', 'watermelon', 'strawberry']

The above example shows how we use the append() method to add the string “strawberry” at the end of my_list. Finally, the updated list is printed, which includes “strawberry” as the sixth element.

 

Example 2: Join Character String to List via ‘+’ operator

In this example, we will use the ‘+’ operator to concatenate a character string to my_list. Take a look at the code below.

my_list = my_list + ["strawberry"]
print(my_list)
# ['apple', 'banana', ' grapes', 'cherry', 'watermelon', 'strawberry']

Great! We have the character string “strawberry” in my_list.

Example 3: Join Character String to List via extend() Method

The extend() method is another alternative for extending the existing list with a new character.

my_list.extend(["strawberry"])
print(my_list)
# ['apple', 'banana', ' grapes', 'cherry', 'watermelon', 'strawberry']

Again, we obtain a satisfactory result!

Example 4: Join Character String to List via insert() Method

This last example shows how to insert a character string to a list using the insert() method and the len() function. See below.

my_list.insert(len(my_list), "strawberry")
print(my_list)
# ['apple', 'banana', ' grapes', 'cherry', 'watermelon', 'strawberry']

As shown in this example, we have passed the length of my_list as the first and the “strawberry” string as the second argument to the insert() method, which has inserted the new string at the end of the list.

 

Video, Further Resources & Summary

Do you need more explanations on how to join a character string to a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

The YouTube video will be added soon.

 

In addition, you may want to have a look at the related programming tutorials on our website:

This post has shown how to add a character string to a list in Python. In case you have further questions, don’t hesitate to leave a comment.

 

Paula Villasante Soriano Statistician & R Programmer

This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get more information about her academic background and the other articles she has written for Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top