How to Use Python to Multiply Strings

How to Use Python to Multiply Strings
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, or 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:

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


The output for the code above would be:


This works, obviously, but it's not perfect if you don't want your multiplied string to read as one large, giant string. 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:


Much more legible.

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)

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!