13

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

$str = "x" x 5;

Can I do something similar in Python?

smci
  • 32,567
  • 20
  • 113
  • 146
Mat
  • 82,161
  • 34
  • 89
  • 109
  • Related: [Create list of single item repeated N times](https://stackoverflow.com/questions/3459098/create-list-of-single-item-repeated-n-times) – smci Oct 10 '19 at 03:11

3 Answers3

31
>>> "blah" * 5
'blahblahblahblahblah'
Dustin
  • 89,080
  • 21
  • 111
  • 133
  • 1
    @S.Lott: So right. SO is quickly becoming the lazy programmer's alternative to reading any documentation. – davidavr Jan 31 '09 at 02:45
  • I agree, but I don't see the harm in that. (I had to chuckle when I saw S.Lott's example!) – j_random_hacker Feb 01 '09 at 13:16
  • 1
    "Dumb programming questions, answered by dumb programmers." A Google search for 'python equivalent perl x' now finds this page followed by Dustin's friendfeed, then nothing. I couldn't find the right search terms for the documentation. – Mat Feb 01 '09 at 14:45
  • 4
    I've also noted that I get more reputation points from asking dumb questions, than from complicated ones that require a bit of thought. I guess its just a flaw in the reputation system. – Mat Feb 01 '09 at 14:46
  • @Mat: Note, I didn't say it was a dumb question. By "lazy programmer" I meant that people aren't looking very hard or experimenting in the interpreter very much before they post to SO. Maybe that's a good thing because it shows that SO is very effective at answering straightforward questions. – davidavr Feb 02 '09 at 14:34
  • @davidavr good news, even people who DO search the manual, pouring over the string library docs can't find this feature. Because it's not in the string docs. It's also not in anything linked to the string docs. In fact, as far as I can tell, it's not anywhere in the docs. "Playing around with the interpreter" is no substitute for documenting a feature. – Robert P Jun 22 '11 at 20:43
  • It's in the [sequence types](https://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange) docs. Which is where the str type is documented, as linked in the first paragraph of the documentation for the string module. – Yann Vernier Jan 22 '15 at 10:56
  • Both Larry Wall and Bill gates said something along the lines of laziness being a virtue to a programmer – wobbily_col Jul 16 '15 at 07:20
  • For all the complaints over 9 years it's funny there's only been one attempt to provide the "not lazy" answer… and, yeah, I could have edited your answer… – dlamblin Dec 25 '18 at 12:05
1

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'
kevinarpe
  • 20,319
  • 26
  • 127
  • 154
0

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:

  • Strings (Okay, yes)
    • Perl "ab"x5 to produce "ababababab"
    • Python "ab"*5 for the same.
  • Lists
    • Perl @ones = (1) x @ones assigns each array element & doesn't reassign the reference.
    • Python ones = [1] * len(ones) look like the same result, but reassigns the reference.
  • Lists of lists:
    • Perl (0)x5 to produce ((0),(0),(0),(0),(0)).
    • Python, almost: [[0]]*5 is [[0],[0],[0],[0],[0]]
  • Dicts / Hashes:
    • Perl: it doesn't seem to be supported with a hash. You'd need to convert to lists and back.
    • Python: also doesn't seem to be supported on a dict.

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
dlamblin
  • 43,965
  • 20
  • 101
  • 140