refer to Print's official documentation
print(...)print(value, ..., sep=‘ ‘, end=‘\n‘, file=sys.stdout, flush=False)Prints the values to a stream, or to sys.stdout by default.Optional keyword arguments:file: a file-like object (stream); defaults to the current sys.stdout.sep: string inserted between values, default a space.end: string appended after the last value, default a newline.flush: whether to forcibly flush the stream.
In Python, print prints the specified text to the screen by default, for example:
>>>print(‘hello,world‘)
hello world
The full format of print is print(objects,sep,end,file,flush)
, with the following 4 optional parameters
- Sep
Inserts the specified string between the output strings, which by default is a space, for example:
>>>print("a","b","c",sep="**")
a**b**c
- End
The print
specified string is added at the end of the output statement, and the default is newline (\ n), for example:
>>>print("a",end="$")
a$
Print by default is a newline, that is, the output statement automatically switch to the next line, for Python3, if you want to implement the output does not wrap the function, then you can set end= "(Python2 can be added after the print statement," to implement the function of non-line wrapping)
- File
The text is entered into the File-like object, which can be a file, a data stream, and so on, by default Sys.stdout
>>>f = open(‘abc.txt‘,‘w‘)
>>>print(‘a‘,file=f)
- Flush
The flush value is true or FALSE, which defaults to Flase, which indicates whether the output statement is immediately entered into the object pointed to by the parameter file (by default, Sys.stdout), for example:
>>>f = open(‘abc.txt‘,‘w‘)
>>>print(‘a‘,file=f)
You can see that the Abc.txt file is empty at this point and f.close()
the content is written to the file only after execution.
If you change to:
>>>print(‘a‘,file=f,flush=True)
You can immediately see the contents of the file
Description of Python print parameters