How to Append or Add String to List Elements in Python

Last Updated on June 7, 2021 by Roshan Parihar

In this tutorial, learn how to append or add string to list elements using Python. The short answer is to use the plus (+) operator of Python and enclose the string variable within square brackets.

You can also add the string to the end of list items or on a specified position. Let’s find out with the example given here below to learn the methods.

Add Single or Multiple Strings to List Using Plus (+) Operator in Python

Add Single String to List in Python

You can add the string to a list elements using the plus(+) operator between the variables. The most noteworthy thing here is that you have to enclose the string variable within square brackets. See the example given below to get the idea of using the method.

Output

[‘Hello World!’, ‘One’, ‘Two’, ‘Three’]

The above example contains the output that shows the added elements. It adds the content to the start position. Initially, there are three items on the list. After the addition, the elements are increased to four.

How to Add Multiple Strings to List Elements in Python

The above example can be useful to add multiple strings to list elements. You just need to use the plus(+) operator and enclosed all the string variables within the square brackets.

Let’s find out how to add multiple strings to a list in Python with the example given below.

Output

[‘Hello World!’, ‘One’, ‘Two’, ‘Three’, ‘Welcome to TutorialDeep!’]

Append String to the End of List Elements in Python

To append a string to the end of the list elements, you have to use the append() function. It takes a single argument as the string variable as given below:

Output

[‘One’, ‘Two’, ‘Three’, ‘Hello World!’]

The above example shows the content added to the end location. The append function always adds the string to the last position of the list.

There are three items in a list at the start of the example. However, after adding the content, it becomes four items.

Insert String to List Items in a Specified Position Using Python

In addition to the above methods, you can use insert() to insert string to list elements at the specified position. It is useful to set the location where you want to place the content.

Add String to the Specified Location in a List in Python

The insert() function takes two arguments to pass. The first argument is the location where you want to place the string. The second argument is the string variable that you have to add to list elements. Let’s see the example given below.

Output

[‘One’, ‘Hello World!’, ‘Two’, ‘Three’,]

The above example shows the output that contains the inserted item to the second position. Placing the string to the required position makes it easier to manage the items easily.

Insert String to the Beginning of List Elements in Python

When you want to insert the string to the beginning of the list in Python, you have to use zero(0) as the first argument of the insert() function.

Output

[‘Hello World!’, ‘One’, ‘Two’, ‘Three’,]

The output shows that the content gets inserted to the first position in the elements.

You can also add the string to the end position using the insert(). However, it requires finding the length of the list to use as the first argument of the function.

So, you have learned many methods above to get the required results. If you think there are many other methods that can be useful for other readers. You can send it to my email id ‘[email protected]’.

I hope you like this post on how to find the value for the given key in Dictionary.

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.