Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Python equivalent of Perl's x operator (replicate string)?

In Perl, I can replicate strings with the 'x' operator:

$str = "x" x 5;

Can I do something similar in Python?

like image 292
Mat Avatar asked Jan 30 '09 20:01

Mat


2 Answers

>>> "blah" * 5
'blahblahblahblahblah'
like image 51
Dustin Avatar answered Sep 21 '22 11:09

Dustin


Here is a reference to the official Python3 docs:

https://docs.python.org/3/library/stdtypes.html#string-methods

Strings implement all of the common sequence operations...

... which leads us to:

https://docs.python.org/3/library/stdtypes.html#typesseq-common

Operation      | Result
s * n or n * s | n shallow copies of s concatenated

Example:

>>> 'a' * 5
'aaaaa'
>>> 5 * 'b'
'bbbbb'
like image 1
kevinarpe Avatar answered Sep 24 '22 11:09

kevinarpe