Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 288
tueda Avatar asked Dec 29 '25 21:12

tueda


1 Answers

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.', '...']

EDIT: As @user2357112 pointed out in the comments and in their answer changing the prompt does not affect this. Here it seemed to work because adding more lines to the beginning of the cell make Ipython interpreter think they are no longer part of the prompt. You can change your string to '\n...\n.o.\n...' as a workaround.

like image 88
Asocia Avatar answered Jan 01 '26 12:01

Asocia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!