If you want the string to repeat up to n characters, then you can first repeat the entire string up to n / len (s) times and add n% len (s) characters at the end. For example,
def repeat_n (string, n): l = len (s) full_rep = n / l # Construct string with full repetitions ans = ’’ .join (string for i in xrange (full_rep)) # add the string with remaining characters at the end. return ans + string [: n% l] repeat_n (’asdf’, 10)
This will give the output:
’asdfasdfas’
You can also use the operation & # 8216; * & # 8217; for a line to repeat the line. For example,
This will give the output:
’asdfasdfas’