Home > OS >  Why does splitlines() not give the expected result for triple dots in Jupyter?
Why does splitlines() not give the expected result for triple dots in Jupyter?

Time:06-18

I believe the following code

s = '''
...
.o.
...
'''
print(s.splitlines())

should print

['', '...', '.o.', '...']

Indeed, this is the case when Python is executed normally (example run on Wandbox is here).

But the reality is ruthless (as usual); Google Colaboratory prints a result without "triple dots":

I also tried the same code with a locally installed Jupyter (Python 3.7.13, Jupyter notebook 6.4.12, IPython 7.34.0) and it gave me the same result as Google Colaboratory.

Does anyone know what causes this deletion of the triple dots?

CodePudding user response:

Google collab interprets ... as part of the prompt. You can change the prompt to some other string and the result will be as you expected:

import sys
sys.ps2 = '<<<' # default value is ...

s = '''
...
.o.
...
'''
print(s.splitlines())
['', '...', '.o.', '...']

CodePudding user response:

I believe that ... at the beginning of the line indicates the secondary prompt in the interactive mode (which is the mode in Jupyter and Colaboratory). You might want to try >>>, it is also a prompt in the interactive mode.

I think that it's better to use the string '\n...\n.o.\n...' instead.

  • Related