7
The question is simple: How do I repeat a string
in Python?
I’m used to PHP.
When I want to repeat a string in PHP, I do so:
var $str = 'StackOverflow';
str_repeat($str, 5);
// Imprime: StackOverflowStackOverflowStackOverflowStackOverflowStackOverflow
I tried that:
'StackOverflow'.repeat(5);
But an error is returned:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'repeat'
Why of
:13
?– Wallace Maxters
You take the part you want to repeat.
– Maniero
o. O! That’s cool! To do this in PHP I would have to use the
substr
andstr_replace
chained! Python is very simple– Wallace Maxters
I put it in a way that works in Python 3 as well. What I like about the language is that it tries most of the time to actually make the codes smaller, simpler.
– Maniero