Ask Your Question
1

how to print without new line

asked 2016-04-04 14:38:11 +0100

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
1

answered 2016-04-04 16:11:48 +0100

slelievre gravatar image

updated 2016-04-04 16:22:29 +0100

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
123

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

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

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

edit flag offensive delete link more
0

answered 2016-04-04 16:06:26 +0100

tmonteil gravatar image

updated 2016-04-04 16:08:44 +0100

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

Stats

Asked: 2016-04-04 14:38:11 +0100

Seen: 12,329 times

Last updated: Apr 04 '16