Repeat String N Times Using a Loop

Easy ⏱ 8 min 82% acceptance ★★★★★ 4.6
Write a function repeat_string(s, n) that builds and returns s repeated n times concatenated together, using a for loop and string concatenation (do not use the * repetition operator).

Examples

Example 1
Input
repeat_string("ab", 3)
Output
"ababab"
Explanation

"ab" concatenated three times.

Example 2
Input
repeat_string("x", 0)
Output
""
Explanation

Zero repetitions yields an empty string.

Constraints

  • n is a non-negative integer.
  • Must not use the `*` string repetition operator.

Topics

Loopsfor loop

Companies

UberAirbnb

Hints

Hint 1

Start with an empty result string and loop n times, appending s each time.

Hint 2

range(n) gives you exactly n iterations.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.