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
1 2 3 4 5 6 7 8 9 |
#Define string and list variables myStr = "Hello World!"; list1 = ["One", "Two", "Three"]; #Add string to list list2 = [myStr] + list1 #Print result print(list2) |
Output
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
1 2 3 4 5 6 7 8 9 10 |
#Define string and list variables myStr1 = "Hello World!"; myStr2 = "Welcome to TutorialDeep!"; myList = ["One", "Two", "Three"]; #Add string to list list2 = [myStr1] + myList + [myStr2] #Print result print(list2) |
Output
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
1 2 3 4 5 6 7 8 9 |
#Define string and list variables myStr = "Hello World!"; myList = ["One", "Two", "Three"]; #Append string to the end of the list myList.append(myStr) #Print result print(myList) |
Output
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
1 2 3 4 5 6 7 8 9 |
#Define string and list variables myStr = "Hello World!"; myList = ["One", "Two", "Three"]; #Insert string to list in a specified position myList.insert(1, myStr) #Print result print(myList) |
Output
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
1 2 3 4 5 6 7 8 9 |
#Define string and list variables myStr = "Hello World!"; myList = ["One", "Two", "Three"]; #Insert string to list in a specified position myList.insert(0, myStr) #Print result print(myList) |
Output
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.