Ask Your Question

how to print without new line

asked 2016-04-04 07:38:11 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

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]),'}'
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2016-04-04 09:11:48 -0500

updated 2016-04-04 09:22:29 -0500

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:

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

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

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('')

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

edit flag offensive delete link more

answered 2016-04-04 09:06:26 -0500

tmonteil gravatar image

updated 2016-04-04 09:08:44 -0500

You can just add a comma after the print command:

sage: print("Def: R "),
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2016-04-04 07:38:11 -0500

Seen: 1,768 times

Last updated: Apr 04 '16