Open In App

Create a List of Strings in Python

Last Updated : 10 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

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

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.

Python
# 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().

Python
# 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.

Python
# 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.

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

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

Python
# Repeating a string multiple times using the * operator

g = ["gfg"] * 3
print(g)

Output
['gfg', 'gfg', 'gfg']

Practice Tags :

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy