In Perl, I can replicate strings with the 'x' operator:
$str = "x" x 5;
Can I do something similar in Python?
In Perl, I can replicate strings with the 'x' operator:
$str = "x" x 5;
Can I do something similar in Python?
>>> "blah" * 5
'blahblahblahblahblah'
 
    
    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'
 
    
    In Perl (man perlop) x is called the repetition operator.
In Python 3 this * is also referred to as a repetition operator.
In Python 2 it is probably called the same thing, but I only found it referred to as sequence repetition under built in operators.
I think it's important to digress on Strings being not the only thing the operator is for; here's a few more:
"ab"x5 to produce "ababababab""ab"*5 for the same.@ones = (1) x @ones assigns each array element & doesn't reassign the reference.ones = [1] * len(ones) look like the same result, but reassigns the reference.(0)x5 to produce ((0),(0),(0),(0),(0)).[[0]]*5 is [[0],[0],[0],[0],[0]]However as implied by the "almost" above, there's a caveat in Python (from the docs):
>>> lists = [[]] * 3
>>> lists
[[], [], []]
>>> lists[0].append(3)
>>> lists
[[3], [3], [3]]
Also in Perl I'm not sure where it's documented but the empty list behaves a bit differently with the operator, probably because it has an equivalency to False.
@one=((1))x5;
say(scalar @one); # 5
@arr=(())x5;
say(scalar @arr); # 0
