আপনি যদি স্ট্রিংটি n অক্ষরে পুনরাবৃত্তি করতে চান, তাহলে আপনি প্রথমে পুরো স্ট্রিংটি n/len(s) বার পুনরাবৃত্তি করতে পারেন এবং শেষে n%len(s) অক্ষর যোগ করতে পারেন। উদাহরণস্বরূপ,
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) এটি আউটপুট দেবে:
'asdfasdfas'
আপনি স্ট্রিং পুনরাবৃত্তি করতে '*' অপারেশন ব্যবহার করতে পারেন. উদাহরণস্বরূপ,
def repeat_n(string_to_expand, n):
return (string_to_expand * ((n/len(string_to_expand))+1))[:n]
repeat_n('asdf', 10) এটি আউটপুট দেবে:
'asdfasdfas'