Sometimes, while working with Python, users encounter problems which it is required to add elements to a list. Python lists are a common data type that is used to store data. It can contain different data types and is also mutable. On the other hand, the Python strings are inside the quotes either single or double.
This post illustrated the multiple techniques to add the string to the list.
How to Add String to List in Python?
To add a string to the list, the following methods are used:
Method 1: Add String to List in Python Using “+” Operator
The easiest way to add the string to the list is the “+” operator which is used to convert the provided string into the list in Python.
Example
First, declare and initialize the list with numeric values:
Then, create a string type variable and pass a string:
Now, call the “print()” statement to check the element of the list as well as the value of the string variable:
print("Original String : " + str(testString))
Then, use the “+” operator to add the string to the list as an element:
Finally, use the print statement to check the final result of the list:
Output
Method 2: Add String to List in Python Using “insert()” Method
Another easiest method to convert any Python string into a Python list is the “insert()” method. It is used to add/insert the element at the end of the provided Python list by utilizing its length as the index number.
Example
Use the “len()” function to get the length of the list and store it in the “indexnum” variable:
Call the “insert()” function along with the required string and list the index as a parameter:
To get the resultant list, use the print statement:
As you can see, the provided string has been added to the list as its element successfully:
Method 3: Add String to List in Python Using “extend()” Method
The “extend()” method is also utilized for adding the string into the list in Python by merging one list with the last of the other list.
Example
Call the “extend()” method along with the required string as a list that needs to add to the list as a parameter:
Use the “print()” function to display the required result:
Output
Method 4: Add String to List in Python Using “itertool.chain()” Method
Python has multiple libraries which contain different functions for performing several operations. The “itertools” is one of them. It includes the “itertools.chain()” method which can be used for concatenating the provided list with any string element.
Example
First, import the “itertools” library:
Then, call its “itertools.chain()” method and pass a list and string variable as a list to it:
To show the result, use the print statement:
Output
That’s all! We have described the different ways of adding strings to the list.
Conclusion
To add the string to the list, the “+” operator, the “insert()” method, the “extend()” method, and the “itertool.chain()” method can be used. All methods and operators are Python’s built-in functionalities. This post illustrated the multiple techniques to add the string to the list.