- 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
Python doesn’t have a built-in array type. The equivalent of an array in Java is a list in Python. So, we slightly modify the problem formulation to create a list.
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) # ['', '', '', '', '']
Background: An Introduction to the Asterisk Operator in Python
Method 2: For Loop
The canonical approach is to create an empty list and iteratively append the same string value to the existing list within a for loop. You can use Python’s standard
# 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) # ['' '' '' '' '']
Background: NumPy — Everything You Need to Know to Get Started
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!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. 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?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming 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.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn 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 that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a 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.