Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python string with three dots after newline ... what is this thing?

I just saw this for the first time today. What is this three dots thing called and what is it for? My guess was to help improve readability. Here is the example I saw:

g = """
... S -> NP VP
... PP -> P NP
... NP -> Det N | Det N PP | 'I'
... VP -> V NP | VP PP
... Det -> 'an' | 'my'
... N -> 'elephant' | 'pajamas'
... V -> 'shot'
... P -> 'in'
... """

this is what it outputs:

"\nS -> NP VP\nPP -> P NP\nNP -> Det N | Det N PP | 'I'\nVP -> V NP | VP PP\nDet -> 'an' | 'my'\nN -> 'elephant' | 'pajamas'\nV -> 'shot'\nP -> 'in'\n"

Edit. The IPython interpreter appears to remove the "... " after a newline while the regular python interpreter does not. This must be something specific to IPython then and not a language feature after all (maybe to simplify copy-pasting from the interpreter?)

Alternative '...' usage in python. I just saw the ... in a different context the other day for multi-indexed numpy arrays like this: ar[0,...,1]. It does not affect the indexes in the middle.

Here is an example usage:

ar = np.zeros((10,20,30,40,50,60,70,80)) # 8-dimensional array
ar2 = ar[:1,2:6,...,72:]
ar2.shape() # prints (1, 4, 30, 40, 50, 60, 70, 8)
like image 705
anthonybell Avatar asked Feb 21 '26 18:02

anthonybell


1 Answers

The ... is what the interpreter prints when it needs you to continue some sort of multi-line command, like a for loop or a multi-line string literal. It's similar to >>>, and not something you should actually type.

like image 59
user2357112 supports Monica Avatar answered Feb 23 '26 07:02

user2357112 supports Monica