ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 04 Apr 2016 09:11:48 -0500how to print without new linehttps://ask.sagemath.org/question/32984/how-to-print-without-new-line/ I wanna use print, but I want it to skip the new line. In Python 3 you should be able to use print(".",end=""), but it gives back: "SyntaxError: invalid syntax".
What's the problem right there and is there a work arround or different funktion?
print("Def: R ",end = "")
if str(p) is not None:
print \ {',', '.join([str(p) for p in pol]),'}'Mon, 04 Apr 2016 07:38:11 -0500https://ask.sagemath.org/question/32984/how-to-print-without-new-line/Answer by slelievre for <p>I wanna use print, but I want it to skip the new line. In Python 3 you should be able to use print(".",end=""), but it gives back: "SyntaxError: invalid syntax".
What's the problem right there and is there a work arround or different funktion?</p>
<pre><code>print("Def: R ",end = "")
if str(p) is not None:
print \ {',', '.join([str(p) for p in pol]),'}'
</code></pre>
https://ask.sagemath.org/question/32984/how-to-print-without-new-line/?answer=32987#post-id-32987Sage 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:
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
Mon, 04 Apr 2016 09:11:48 -0500https://ask.sagemath.org/question/32984/how-to-print-without-new-line/?answer=32987#post-id-32987Answer by tmonteil for <p>I wanna use print, but I want it to skip the new line. In Python 3 you should be able to use print(".",end=""), but it gives back: "SyntaxError: invalid syntax".
What's the problem right there and is there a work arround or different funktion?</p>
<pre><code>print("Def: R ",end = "")
if str(p) is not None:
print \ {',', '.join([str(p) for p in pol]),'}'
</code></pre>
https://ask.sagemath.org/question/32984/how-to-print-without-new-line/?answer=32986#post-id-32986You can just add a comma after the print command:
sage: print("Def: R "),
Mon, 04 Apr 2016 09:06:26 -0500https://ask.sagemath.org/question/32984/how-to-print-without-new-line/?answer=32986#post-id-32986