Skip to main content

Repeat a string to a certain length in Python.

def repeat_str(str_to_repeat, repeat_length):
    """Repeat a string to certain length.

    :param str_to_repeat: The string to repeat. Normally a single char.
    :param repeat_length: The amount of times to repeat the string.
    :return: The repeated string.
    a, b = divmod(repeat_length, len(str_to_repeat))
    return str_to_repeat * a + str_to_repeat[:b]