← All coding practice

Coding Practice · Beginner · October 23, 2026 · 4 min read

Python Coding Exercise #54: String Multiplied

Part of the series Python Practice Exercise — part 54

Before you start: Know input() and int() · Know print()

The question

Please write a program which asks the user for a string and an amount. The program then prints out the string as many times as specified by the amount. The printout should all be on one line, with no extra spaces or symbols added.

An example of expected behaviour:

Please type in a string: hiya
Please type in an amount: 4
hiyahiyahiyahiya

What is new here: multiplying a string

The * operator multiplies numbers, but it also works on a string. When a string is multiplied by a whole number, Python repeats the string that many times and joins the copies together:

print("ab" * 3)
ababab

So the repeating is done by a single *, with no loop. The result is one longer string, and nothing is added between the copies. That is exactly what the exercise asks for: all on one line, with no extra spaces or symbols.

The number has to be a whole number. The order doesn’t matter, since 3 * "ab" gives the same ababab.

Before coding: the amount must be an integer

input() always returns a string, even when the user types a number. If the amount is left as a string, Python can’t use it to repeat the text:

word = input("Please type in a string: ")
number = input("Please type in an amount: ")
print(word * number)
TypeError: can't multiply sequence by non-int of type 'str'

So the amount is converted with int(), as in Exercise #08. The string stays a string, because that is what gets repeated.

My Solution

word = input("Please type in a string: ")
number = int(input("Please type in an amount: "))
words = word * number
print(words)

How My Solution Works

1. Ask for the word

word = input("Please type in a string: ") stores whatever the user types as text. It stays text, so no conversion is needed.

2. Ask for the amount, as an integer

number = int(input("Please type in an amount: ")) asks for the amount and converts it to an integer straight away.

3. Multiply

words = word * number repeats word as many times as number says, and stores the long result in a new variable, words. For hiya and 4, words is hiyahiyahiyahiya.

4. Print

print(words) shows the result. print() adds one line break at the end, and nothing else, so the printout is on one line with no extra spaces.

University Solution

input_string = input("Please type in a string: ")
amount = int(input("Please type in an amount: "))
print (input_string * amount)

The University Solution is the same program, with two small differences:

  • No extra variable. It multiplies inside the print(), so the repeated string is printed without being stored first. My Solution stores it in words and then prints it, which does the same job in an extra line.
  • Different names, such as input_string and amount.

The print ( has a space before the bracket. Python allows that, and it doesn’t change the output, but print( without the space is the usual way to write it.

Both programs print identical output for the sample and for all the other inputs tested.

Things to notice

An amount of 0, or less, prints an empty line. Repeating a string zero times gives an empty string, and so does a negative amount. Then print() still adds its line break, so the program shows a blank line.

Spaces inside the string are repeated too. The program adds no spaces of its own, but if the user types a string that has a space in it, such as a b, that space is copied with everything else. An amount of 3 gives a ba ba b.

A string made of digits stays a string. If the user types 4 as the string and 3 as the amount, the result is 444, not 12. Only the amount is converted with int(), so the 4 is repeated as text.

The length is multiplied too. The result has as many characters as the string’s length times the amount. hiya has 4 characters, so hiya repeated 4 times has 16.

Key takeaways

  • * repeats a string: "ab" * 3 gives ababab.
  • The repeated copies are joined together with nothing in between.
  • The amount has to be a whole number, so convert the input with int().
  • input() always gives a string, so the text needs no conversion.
  • An amount of 0 gives an empty string.
  • You can print the result directly, or store it in a variable first. Both give the same output.

✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦


Practice Source: This exercise is taken from the Python Programming MOOC 2026 by the University of Helsinki. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.

Tagged: python · strings · string-multiplication · input