Creating a String Array — What’s the Most Pythonic Way?

Problem Formulation

  • Given an integer number n.
  • Given an initial string value s.

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
# ['', '', '', '', '']

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 list.append(element) function.

# Method 2: For Loop
a = []
for i in range(n):
# ['', '', '', '', '']

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)]
# ['', '', '', '', '']

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 np.array() constructor.

# Method 4: NumPy
import numpy as np
a = np.array([s for _ in range(n)])
# ['' '' '' '' '']

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!

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.

Join the free webinar now!