Where did the new line appear in Python?

In Python, when I do

print "Line 1 is"
print "big"

The output I get is

Line 1 is
big

Where does the new line come from? And how do I enter both operators on the same line using two print statements?

+3
source share
4 answers

printadds a new line by default. To avoid this, use trailing ,:

print "Line 1 is",
print "big"

,still give a space. To avoid a space, group your lines and use a single statement printor use sys.stdout.write()instead.

+16
source

From the documentation :

A '\n' end, . .

+4

If you need full control over the bytes written to the output, you can use sys.stdout

import sys
sys.stdout.write("Line 1 is ")
sys.stdout.write("big!\n")

If you do not output a new line ( \n), you need to explicitly call flush so that your data is not buffered like this:

sys.stdout.flush()
+4
source

this is standard functionality use print "foo",

+2
source

Source: https://habr.com/ru/post/1784956/


All Articles