Ask Your Question

Revision history [back]

Sage uses Python 2.7. You can add a comma at the end of your print statement to avoid a newline.

However you will still get a space, and I don't know how to avoid that.

sage: for a in [1, 2, 3]: print a,
1 2 3

One workaround is to build a string incrementally, and only print it when it's done.

sage: s = ''
sage: for a in [1, 2, 3]: s += str(a)
sage: print s
123

Sage uses Python 2.7. You can add a comma at the end of your print statement to avoid a newline.

However you will still get a space, and I don't know how to avoid that.

sage: for a in [1, 2, 3]: print a,
1 2 3

One workaround is to build a string incrementally, and only print it when it's done.

sage: s = ''
sage: for a in [1, 2, 3]: s += str(a)
sage: print s
123

The migration to Python 3 is tracked here:

http://trac.sagemath.org/ticket/15530

Sage uses Python 2.7. You can add a comma at the end of your print statement to avoid a newline.

However you will still get a space, and I don't know how to avoid that.

sage: for a in [1, 2, 3]: print a,
1 2 3

One workaround is to build a string incrementally, and only print it when it's done.

sage: s = ''
sage: for a in [1, 2, 3]: s += str(a)
sage: print s
123

The For other workarounds, searching the web for "python print without newline or space" yields a number of results among which:

  • http://stackoverflow.com/questions/493386/how-to-print-in-python-without-newline-or-space
  • http://stackoverflow.com/questions/4499073/printing-without-newline-print-a-prints-a-space-how-to-remove

Note: the migration of SageMath to Python 3 is tracked here:

http://trac.sagemath.org/ticket/15530

Sage uses Python 2.7. You can add a comma at the end of your print statement to avoid a newline.

However you will still get a space, and I don't know how to avoid that.space:

sage: for a in [1, 2, 3]: print a,
1 2 3

One workaround is to build a string incrementally, and only print it when it's done.

sage: s = ''
sage: for a in [1, 2, 3]: s += str(a)
sage: print s
123

For other workarounds, searching the web for "python print without newline or space" yields a number of results among which:

  • http://stackoverflow.com/questions/493386/how-to-print-in-python-without-newline-or-space
  • http://stackoverflow.com/questions/4499073/printing-without-newline-print-a-prints-a-space-how-to-remove

In particular you can get the Python3-like print function:

sage: from __future__ import print_function
sage: for a in [1, 2, 3]:
....:     print(a, end='')
....: print('')
123

Note: the migration of SageMath to Python 3 is tracked here:

http://trac.sagemath.org/ticket/15530