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

3

13

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

$str = "x" x 5;

Can I do something similar in Python?

Distefano answered 30/1, 2009 at 20:27 Comment(1)
Related: Create list of single item repeated N timesTildi
A
31
>>> "blah" * 5
'blahblahblahblahblah'
Alary answered 30/1, 2009 at 20:29 Comment(9)
@S.Lott: So right. SO is quickly becoming the lazy programmer's alternative to reading any documentation.Auricle
I agree, but I don't see the harm in that. (I had to chuckle when I saw S.Lott's example!)Rugging
"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.Distefano
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.Distefano
@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.Auricle
@Auricle 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.Nihilism
It's in the sequence types docs. Which is where the str type is documented, as linked in the first paragraph of the documentation for the string module.Uziel
Both Larry Wall and Bill gates said something along the lines of laziness being a virtue to a programmerBookplate
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…Chanticleer
G
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'
Gunas answered 23/5, 2015 at 6:43 Comment(0)
C
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
Chanticleer answered 25/12, 2018 at 12:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.