"
This article is part of in the series
Last Updated: Friday 22nd September 2023

How to Use Python to Multiply Strings

We've already gone over how to use multiplication in Python, but did you know that Python can be used to multiply things other than numbers? In fact, you can use Python to multiply strings, which is actually pretty cool when you think about it. You can take a string and double, triple, even quadruple it with only a little bit of Python.

There are a few different ways that we can go about multiplying strings, depending on how you want your multiplied strings to be formatted. Take a look at the code snippets below to see how it works:

#1 Using the Multiplication Operator

In Python, the multiplication operator * isn't just for numbers. It's a lot more versatile – you can use it to replicate lists and strings, among other things. 

For this section, think of it as a way to quickly duplicate or repeat strings. The good news is, Python makes it super simple to do this. Let's take a look at the straightforward syntax:

new_string = original_string * times_to_repeat

In this syntax:

  • new_string: This is where the repeated string will be stored.
  • original_string: This is the string you want to duplicate.
  • times_to_repeat: This is how many times you'd like the original string to be repeated.

But here's what's interesting:

To simply multiply a string, this is the most straightforward way to go about doing it:

2*'string'

The output for the code above would be:

stringstring

In the above example, we're taking the direct approach by multiplying the string with the number. But as discussed, you can take the more sophisticated approach of storing a string in a variable and apply the same technique. Here's an example:

 

original_string = "Word"

# We can now follow the syntax 
# discussed above

new_string = original_string * 3

# Let's check the result by printing out the strings

print(f"Original string is: {original_string}")
print(f"New string is: {new_string}")

The output of this code is:

Original string is: Word New string is: WordWordWord

You can go the extra mile with this approach if you don't want to repeatedly type the same characters (or words) and let Python do the work. You can instruct Python to create a desired string using the arithmetic operators of the programming language.  Let's break this down with an example. If you need to print a string with several "*" characters, you can simply tell Python the number of times you want the characters printed and concatenate the rest of the string.    Here's how this would work:

welcome_message = "*" * 10 + "Hello and welcome to this program!" + "*" * 10

As you might have guessed, the output of this code looks like this:

********** Hello and welcome to this program! **********

This works, obviously, but it's not perfect if you don't want your multiplied string to read as one large, giant string. This brings us to the second method of multiplying strings in Python.

#2 Multiplying a String Stored in a List or Tuple

If printing out the output of string multiplication as one string looks unsophisticated or doesn't fit your use case, it's a good idea to put the string in a list (or tuple) before multiplying it.  As you'd expect, this approach will give you a list with the string multiplied by the number of times you've instructed Python to. Let's take a look at the syntax:

new_list = ["sample_string"] * times_to_repeat

In this syntax:

    • "sample_string": This is the string you want to multiply.
    • times_to_repeat: Specifies how many times you want the string to be duplicated in the list.

Let's have a look at this syntax in action:

# Initializing a list
sample_string = ["apple"] 

# Defining the number of copies needed in the new list 
new_list = sample_string * 4 

# Checking the result 
print(new_list)   

# print the list 
print(f"Original list is: {sample_string} ") 
print(f"List after multiplication is: {new_list}")

Here's the output of this code:

Original list is: ["apple"]  List after multiplication is: ['apple', 'apple', 'apple', 'apple']

For this operation, the time complexity is O(n), with n being the number of repetitions of the original list. In simple terms, the more times we duplicate the list, the longer it will take, growing linearly. The auxiliary space complexity is O(n). Again, n represents the number of repetitions of the original list. This means that the more repetitions we create, the more memory space we'll require, increasing in a linear fashion. Of course, you can use the same method if you're storing your string in a tuple. If you want your strings to be separated and not just read as one long word, you'll have to change the code up a bit, and change your string to a tuple, like this:

4*('string',)

The output for the code above would be:

('string', 'string', 'string', 'string')

Much more legible.

But here's what's more interesting:

You can also use Python to multiply sets of words, strings, or tuples. Check out the code snippet below to see how it's done:

3*('good', 'morning')

The output for the code above would look like this:

('good', 'morning', 'good', 'morning', 'good', 'morning)

#3 Using a for Loop

Another simple approach to multiplying strings is by using a for loop. 

  1. Set up Original String: Begin with your intended string. Let's call it original_string.
  2. Determine Repetitions: Decide how many times you want the string to be repeated and store this value in a variable named repetitions.
  3. Initialize an Empty String: Create an empty string, named result_string, to store the repeated string.
  4. Iterative Concatenation: Run a for loop, iterating repetitions times. On each cycle, append original_string to result_string.
  5. Display the Result: Once the loop completes, print out result_string to see the original string repeated.

Let's see this algorithm in action:

# Define the original string and the number of repetitions.
string = "Original"
n = 3

# Initialize an empty string to store the result.
new_string = ""

# Use a for loop to concatenate the original string 'n' times.
for i in range(n):
    new_string += string

# Print the resulting repeated string.
print(new_string)  

Here's what the output of this code looks like:

OriginalOriginalOriginal

For this operation, the time complexity is O(n), with n being the number of repetitions of the original list. In simple terms, the more times we duplicate the list, the longer it will take, growing linearly.

The space complexity is also O(n). Again, n represents the number of repetitions of the original list. This means that the more repetitions we create, the more memory space we'll require, increasing in a linear fashion.

#4 Multiplying Empty Strings

It's interesting to question what happens if you multiply an empty string. The answer to the question becomes clear when you consider that empty strings are essentially void values occupying zero characters. 

Multiplying nothing with any number will give you nothing. Let's validate this idea with a piece of code:

answer = "" * 10

Running this code will return nothing – an empty string.

As you're probably starting to see, using Python to multiply strings isn't complicated at all. It's pretty cool that you can use the same concept you'd use to multiply numbers (our handy * symbol) to multiply words and other types of objects. Sadly, this same concept doesn't really work with division the same way it does with multiplication, but you can do something similar with addition -- but that's for another tutorial!