I have a piece of code that will take a string and repeat it such that the length of the string is x.
>>> import math
>>> def repeat(data, length):
return (data * int(math.ceil(float(length) / len(data))))[:length]
>>> repeat("Hello World", 22)
'Hello WorldHello World'
>>> repeat("Hello World", 20)
'Hello WorldHello Wor'
Is there any way to optimize it? I need this operation to be fast, as it will be used a lot. Note that this also needs to work with lists.