String Repetition and Separation in Python

Last Updated : 3 Nov, 2026

Given three inputs a, b, and c, print a exactly a times and b exactly b times in the same line, with c printed between them.

Examples:

Input: a = 6, b = 4, c = &
Output: 666666&4444
Explanation: 6 printed 6 times and 4 printed 4 times separated by c = &.

Input: a = 1, b = 3, c = #
Output: 1#333
Explanation: 1 is printed once and 3 is printed 3 times, with # between them.

The idea is to repeat the string a exactly a times and the string b exactly b times, then prints them on one line with c placed between them as the separator.

This works because of two built-in features of Python:

  1. Operator overloading: The * operator repeats a string when used between a string and an integer. So a * int(a) repeats a as many times as its own numeric value, and b * int(b) does the same for b. We use int() because * can’t work between two strings.
  2. sep parameter of print(): By default, print() puts a space between multiple values. Here sep=c places c between the two repeated strings instead, so the final output is the repeated a, then c, then the repeated b.
Python
# Driver Code
if __name__ == "__main__":
    a = '6'
    b = '4'
    c = '&'

    print(a * int(a), b * int(b), sep=c)

Output
666666&4444
Try It Yourself
redirect icon
Comment