- Given an integer number
- Given an initial string value
How to create an array of
n copies of
s in Python?
# Input: n = 5 s = '' # Output: ['', '', '', '', '']
Method 1: List Multiplication
The most Pythonic way to create a string array in Python of string
s and size
n is to use the list multiplication asterisk operator in the expression
[s] * n.
# Method 1: List Multiplication a = [s] * n print(a) # ['', '', '', '', '']
Method 2: For Loop
# Method 2: For Loop a =  for i in range(n): a.append(s) print(a) # ['', '', '', '', '']
Background: Python Loops
Method 3: List Comprehension
List comprehension is a concise way to create a new list in a single line. List comprehension is a compact way of creating lists. The simple formula is
[expression + context].
- Expression: What to do with each list element?
- Context: What elements to select? The context consists of an arbitrary number of for and if statements.
Here’s an example:
# Method 3: List Comprehension a = [s for _ in range(n)] print(a) # ['', '', '', '', '']
Background: An Introduction to List Comprehension
Method 4: Creating a NumPy Array
NumPy is Python’s standard library for numerical computations. The closest thing to a Java array in Python is a NumPy array. You can create a NumPy array from any Python list by passing the list into the
# Method 4: NumPy import numpy as np a = np.array([s for _ in range(n)]) print(a) # ['' '' '' '' '']
The string data type in Python is immutable. This means that you cannot change a string after creating it. This is unlike Java where you can change a string after creating it. Therefore, it often doesn’t make sense to initialize an array with some default strings because those strings cannot be modified later on. The only thing you can do is to overwrite the strings in the list. But if you were doing this, adding the dummy value doesn’t make a lot of sense in the first place.
Background Video List Comprehension
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.