Ask Your Question

Revision history [back]

The display of lists gets extra newline characters when they go over a certain number of elements. But you can get a common behaviour by using "print".

sage: print(range(22))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
sage: print(range(23))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]

The display of lists gets extra newline characters when they go over a certain number of elements. elements.

But you can get a common unified behaviour by using "print".print, str or repr.

sage: print(range(22))
a = list(range(22))
sage: b = list(range(23))

sage: print(a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
sage: print(range(23))
print(b)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]

sage: repr(a)
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]'
sage: repr(b)
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]'

sage: str(a)
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]'
sage: str(b)
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]'

sage: a.__repr__()
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]'
sage: b.__repr__()
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]'

sage: a.__str__()
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]'
sage: b.__str__()
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]'

The display of lists gets seems to get extra newline characters when they go over a certain number of elements.

But you can get a unified behaviour by using print, str or repr.

sage: a = list(range(22))
sage: b = list(range(23))

sage: print(a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
sage: print(b)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]

sage: repr(a)
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]'
sage: repr(b)
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]'

sage: str(a)
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]'
sage: str(b)
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]'

sage: a.__repr__()
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]'
sage: b.__repr__()
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]'

sage: a.__str__()
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]'
sage: b.__str__()
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]'

Edit (after reading @Sébastien's answer and the stackoverflow answer mentioned in it). Note that what makes the pretty printing of the list get extra newline characters is not the length of the list, but whether its string representation goes over 79 characters.

sage: c = list(range(12345678901234, 12345678901239))
sage: len(c)
5
sage: len(str(c))
80
sage: c
[12345678901234,
 12345678901235,
 12345678901236,
 12345678901237,
 12345678901238]
sage: print(c)
[12345678901234, 12345678901235, 12345678901236, 12345678901237, 12345678901238]

The display of lists seems to get extra newline characters when they go over a certain number of elements.

But you can get a unified behaviour by using print, str or repr.

sage: a = list(range(22))
sage: b = list(range(23))

sage: print(a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
sage: print(b)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]

sage: repr(a)
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]'
sage: repr(b)
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]'

sage: str(a)
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]'
sage: str(b)
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]'

sage: a.__repr__()
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]'
sage: b.__repr__()
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]'

sage: a.__str__()
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]'
sage: b.__str__()
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]'

Edit (after reading @Sébastien's answer and the stackoverflow answer mentioned in it). Note that what makes the pretty printing of the list get extra newline characters is not the length of the list, but whether its string representation goes over 79 characters.

sage: c = list(range(12345678901234, 12345678901239))
sage: len(c)
5
sage: len(str(c))
80
sage: c
[12345678901234,
 12345678901235,
 12345678901236,
 12345678901237,
 12345678901238]
sage: print(c)
[12345678901234, 12345678901235, 12345678901236, 12345678901237, 12345678901238]

Note that this was asked before on Ask Sage:

and there is a Sage Trac ticket about this