How to Append or Add String to List Elements in Python

Last Updated on May 12, 2024 by Roshan Parihar

To append or add a string to list elements in Python, use the plus (+) operator 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 String to List Using Plus (+) Operator in Python

If you want to add a string to List in Python, you can use the plus(+) operator. You can add single or multiple strings to a list using this method.

Add Single String to List in Python

You can add a single string to List elements using the plus(+) operator. 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.

Example 1

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.

Add Multiple Strings to List Elements in Python

You just need to use the plus(+) operator and enclose all the string variables within the square brackets. Let’s find out with the example given below.

Example 2

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:

Example 3

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 a 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.

Example 4

Output

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

The above example shows the output that contains the inserted item in the second position. Placing the string in 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.

Example 5

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.

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.