How to repeat a string to up to specific length in Python

1 Answer

0 votes
def repeat_up_to_length(string, length):
    return (string * (length//len(string) + 1))[:length]

print(repeat_up_to_length('python', 22))  




'''
run:

pythonpythonpythonpyth

'''

 



answered Jul 19, 2022 by avibootz

Related questions

1 answer 160 views
2 answers 250 views
2 answers 229 views
229 views asked Dec 2, 2017 by avibootz
3 answers 189 views
189 views asked Apr 24, 2021 by avibootz
2 answers 247 views
...