Create a List of Strings in Python
Creating a list of strings in Python is easy and helps in managing collections of text. For example, if we have names of people in a group, we can store them in a list. We can create a list of strings by using Square Brackets [] . We just need to type the strings inside the brackets and separate them with commas.
a = ["geeks", "for", "geeks"]
# Printing the list
print(a)
Output
['geeks', 'for', 'geeks']
Let's explore various other methods to Create a list of strings in Python.
Table of Content
Using list() Function
Another way to create a list of strings is by using the built-in list() function. We can use list() function to convert other iterable objects (like tuples, sets, etc.) into a list.
# Converting a tuple to a list using the list() function
b = list(("geeks", "for", "geeks"))
print(b)
Output
['geeks', 'for', 'geeks']
Using List Comprehension
List comprehension is a concise and efficient way to create lists, especially when you need to apply some logic or transformations. It is usually faster than using a loop with .append().
# Creating a list of strings using list comprehension
d = ["gfg" + str(i) for i in range(3)]
print(d)
Output
['gfg0', 'gfg1', 'gfg2']
Using a Loop with append()
If we want to add strings to our list dynamically then we can use a loop. This method allows us to build a list of strings based on certain conditions or inputs.
# Using a loop to add strings dynamically to the list
c = []
for i in range(3):
c.append("gfg" + str(i))
print(c)
Output
['gfg0', 'gfg1', 'gfg2']
Using extend() Method
If we already have a list and want to add more strings to it, we can use the extend() method. This method helps to add multiple strings at once.
e = ["geeks", "for"]
# Adding multiple strings to a list using extend()
e.extend(["geeks", "gfg"])
print(e)
Output
['geeks', 'for', 'geeks', 'gfg']
Using + Operator to Combine Lists
We can also combine two lists of strings using the + operator. However, it can be less efficient than using extend() for large lists because it creates a new list each time.
f = ["geeks", "for"]
# Concatenating two lists using the + operator
f = f + ["geeks", "gfg"]
print(f)
Output
['geeks', 'for', 'geeks', 'gfg']
Using * Operator for Repeating Strings
If we need a list with repeated strings, we can use the * operator. This is helpful when we want to create a list with a specific string repeated multiple times.
# Repeating a string multiple times using the * operator
g = ["gfg"] * 3
print(g)
Output
['gfg', 'gfg', 'gfg']