- List of Strings in Python
- Create a List of Strings in Python
- Add String to a List of Strings in Python
- How to add a string to a list in Python
- Add string in list Python
- Method-1: Add string to a list in Python using append() method
- Method-2: Python add string to list using insert() method
- Method-3: Python list add string using extend() method
- Method-4: Add string to list in Python using the + Operator
- Method-5: Add a string in list Python using the * operator for unpacking
- Conclusion
- Add String to a List in Python
- 1. Quick Examples of Adding String to a List
- 2. Add String to List at the End
- 3. Add String at a Specific Position
- 4. Add Multiple Strings to the List
- 5. Add String to List Using + Operator
- 6. Using List Comprehension
- Conclusion
- You may also like reading:
- Python – List of Strings
- Create List of Strings
- Access Strings in List of Strings
- Modify Strings in List of Strings
- Traverse Strings in List of Strings
- Summary
List of Strings in Python
Lists are one of the most used data structures in Python. In this article, we will discuss how to perform different operations on a list of strings in Python.
Create a List of Strings in Python
To create a list of strings, you can use the square brackets along with the string values as follows.
Here, we have created a list of strings where each string is the name of a programming language.
If you have a container object like a set or tuple of strings, you can create the list of strings using the list() constructor. The list() constructor takes a container object as its input argument and returns a list of the elements present in the container object. From a set or tuple of strings, you can create a list of strings in python using the list() constructor as follows.
In the above example, we have created a list of strings using a set of strings and the list() constructor. The order of strings in the list is not the same in the list and set because a set is an unordered container object. Hence, the order in a set doesn’t matter. Due to this, the elements of the set aren’t added to the list in a specific order. Due to this, the order of elements in the list and the set are different.
Add String to a List of Strings in Python
To add a string to a list of strings, you can use the append() method. The append() method, when invoked on a list, accepts the string as the input argument and appends the input string at the end of the list of strings as shown below.
How to add a string to a list in Python
In this Python tutorial, we will see how to add a string to a list in Python with different methods and using practical examples.
In Python, a list is a mutable, or changeable, ordered sequence of elements. Each element inside a Python list is referred to as an item. These items can be of various data types, including strings, which are essentially sequences of characters.
Before we add a string to a list in Python, we first need to have a list. In Python, lists are created by placing a comma-separated sequence of items inside square brackets []. Here’s an example:
# A list of some major cities in the USA usa_cities = ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix']
In this example, usa_cities is a Python list that contains five strings, each representing a major city in the USA.
Add string in list Python
There are various different ways to add string to list in Python. They are:
We will see them one by one using illustrative examples.
Method-1: Add string to a list in Python using append() method
The append() method in Python adds an item to the end of a list. It increases the list’s size by one. It offers a practical method to add one or more elements to a Python list that already exists.
For example, we have USA cities names in a Python list. Let’s try to add a new city in this Python list.
usa_cities = ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix'] usa_cities.append('Philadelphia') print(usa_cities)
The output is: The ‘Philadelphia’ is added in the end of the Python list.
['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix', 'Philadelphia']
This way we can add a string to the list in Python using the append() method.
Method-2: Python add string to list using insert() method
The insert() method can add an item at a specific position in the Python list. The method takes two parameters: the index at which the item will be inserted, and the item itself.
For example, Let’s say we’re tracking the largest states in the USA by area:
large_states = ['Alaska', 'Texas', 'California'] large_states.insert(2, 'Montana') print(large_states)
The output is:
['Alaska', 'Texas', 'Montana', 'California']
This way we can insert () method to add a string to the list in Python.
Method-3: Python list add string using extend() method
The Python’s List extends() method iterates over an iterable like a string, list, etc. If we have multiple strings that you want to add to the list, you can put them in another list and add them using the extend() method.
This method adds the elements of the supplied list to the end of the existing Python list. The list size will increase with the number of items we are adding.
For example, Suppose we have a list of some popular American food and we want to add more items to it:
usa_foods = ['Hot dogs', 'Hamburgers', 'Apple pie'] usa_foods.extend(['BBQ ribs', 'Fried chicken']) print(usa_foods)
The Output is:
['Hot dogs', 'Hamburgers', 'Apple pie', 'BBQ ribs', 'Fried chicken']
This way we can use extend() method in the Python list to add strings.
Note: If we will add a string directly using extend() method, each letter will be considered an individual element. We can see below.
Method-4: Add string to list in Python using the + Operator
In Python, we can use the + operator to combine two lists. This method is straightforward and allows us to add multiple items at once. For concatenation, both items need be to of the same type.
The Python + operator will increase the size of the list by the total number of items in the other list and, the element will be added at the end of the list.
For example, Suppose we’re keeping a list of American Nobel laureates in Literature:
nobel_laureates = ['Ernest O. Lawrence', 'Linus Pauling'] nobel_laureates = nobel_laureates + ['Richard P. Feynman', 'Baruch S. Blumberg'] print(nobel_laureates)
The output is:
['Ernest O. Lawrence', 'Linus Pauling', 'Richard P. Feynman', 'Baruch S. Blumberg']
This way we can use the + operator to add the string to the list in Python.
Method-5: Add a string in list Python using the * operator for unpacking
The * operator can be used to unpack elements from an iterable (like a list or a string) and add them to another Python list. This operator is used to add elements of a string as separate items in a list.
For example, Let’s say we want to add each letter of the string ‘USA’ to a list:
letters = ['Z', 'F', 'C'] country = 'USA' letters = [*letters, *country] print(letters)
The Output is:
This way we can use the * operator to add a string in the list Python.
Conclusion
Adding strings to a Python list is a fundamental operation in Python, and it is crucial for manipulating data within our code. The append(), insert(), and extend() methods provide straightforward ways to add one or more strings to a list, whereas we can use the + and * operator too. thereby enabling the dynamic modification of lists in Python.
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.
Add String to a List in Python
How to add a string or multiple strings to a list in Python? Adding a string to a list is a widespread usage of list operation and it can be done in several ways. For example, you can use the + operator, append() , insert() , extend() , and list comprehension . In this article, I will explain adding string to a list in python by using all these methods with examples.
Below are methods to add or append a string to a list in python:
- Append String at the end of a list.
- Add an element at a specific index in a list.
- Add elements from the list to the end of the list.
- Use + operator to concatenate two string lists together.
- Use list comprehension to add string elements to a list.
1. Quick Examples of Adding String to a List
If you are in a hurry, below are some quick examples of adding a string to a list.
2. Add String to List at the End
By using append() you can add a string at the end of the list in Python. This method takes the string as an argument and adds the string to the existing list, this does not return any value. For example, lets take list called technology and add the string Java using the following code.
This example yields the below output.
Similarly, you can also use the append() to add a list of strings to a list in Python. For example, create a list technology1 and add it to the list technology . Now technology contains the elements of the original list and the list you added.
This example yields the below output. Notice that the list is added as a single element resulting nested list.
3. Add String at a Specific Position
You can use the insert() to add a string at a specific index position in the python list. This takes two parameter, first the index where you watend to add and the string value to be added. Lets add string ‘Hadoop’ at the first position on the list. For more examples refer to add element to list by index.
4. Add Multiple Strings to the List
The extend() adds multiple strings to the existing list at the end. When you add an iterator with stirngs by using this method, it actually extends the strings as separate string and adds them to the list. Note that this modifies the existing list with the new strings and the strings are added at the end of the original list. For more examples refer to append multiple elements to the list.
Let’s add string from the tuple to insert into the list. Here, my list had 2 strings to start with, and added a tuple of 3 stirng, after adding a tuple, there are 5 string in the list.
5. Add String to List Using + Operator
You can also use the «+» operator to concatenate two lists together. For example,
6. Using List Comprehension
You can use list comprehension to add a string to a list based on a certain condition or operation. For example,
Conclusion
This article explained how to add or append a string at the end of a Python list, add a string at a specific index in a list, and add multiple stirngs from the list at the end. Also explained using + operator to concatenate two string lists together and finally using the list comprehension .
You may also like reading:
Python – List of Strings
In Python, List of Strings is a list which contains strings as its elements.
In this tutorial, we will learn how to create a list of strings, access the strings in list using index, modify the strings in list by assigning new values, and traverse the strings in list in a loop using while, for.
Create List of Strings
To create a list of strings, assign the comma separated string values enclosed in square brackets.
Python Program
list_of_strings = ['apple', 'banana', 'mango'] print(list_of_strings)
We can also use list() constructor to create a list of strings as shown below.
Python Program
list_of_strings = list(['apple', 'banana', 'mango']) print(list_of_strings)
Access Strings in List of Strings
To access items in list of strings, we can use index. Index starts from 0, and increments by one for subsequent elements.
Python Program
list_of_strings = ['apple', 'banana', 'mango'] print(list_of_strings[0]) print(list_of_strings[1]) print(list_of_strings[2])
Modify Strings in List of Strings
To modify items in list of strings, we can use index similar to as how we accessed strings in the above example. We have to use assignment operator and assign a new value to the element in list referenced by index.
Python Program
list_of_strings = ['apple', 'banana', 'mango'] list_of_strings[1] = "orange" print(list_of_strings)
Traverse Strings in List of Strings
Strings in List of Strings are ordered collection of items. So, we can use any looping statement: while, for loop.
In the following example, we will use for loop to traverse each string in list of strings.
Python Program
list_of_strings = ['apple', 'banana', 'mango'] for string in list_of_strings: print(string)
In the following example, we will use while loop to traverse each string in list of strings.
Python Program
list_of_strings = ['apple', 'banana', 'mango'] i = 0 while i < len(list_of_strings): print(list_of_strings[i]) i += 1
Summary
In this tutorial of Python Examples, we learned how to create, access, modify and iterate for a list of strings.