5. Python print() function
The print() method is used to print output, which is the most common function.
grammar
Following is the syntax of print() method:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
parameter
objects -- plural, indicating that multiple objects can be output at one time. When outputting multiple objects, they need to be separated by ,.
sep -- used to separate multiple objects, the default value is a space.
end -- used to set what to end with. The default value is the newline character \n, which can be replaced by other strings.
file – The file object to write to.
flush – Whether output is buffered is usually determined by file, but if the flush keyword argument is True, the stream will be forced to flush.
Return Value
none.
Printing a string
print('Hello, world!')
Example results:
Hello, world!
Printing Numbers
print('1024')
Example results:
1024
Print List
L = [1, 2, 3, 4]print(L)
Example results:
[1, 2, 3, 4]
Printing Tuples
L = (1, 2, 3, 4)print(L)
Example results:
(1, 2, 3, 4)
Print Dictionary
D = {'one' : 1,'two': 2}print(D)
Example results:
{'two': 2, 'one': 1}
Print formatted string
print('Hello, {}!'.format('world'))
Example results:
Hello, world!
Print formatted floating point numbers
print('%10.3f' % 3.1415926)
Example results:
3.142
Three keyword arguments
sep
The string inserted between each object text, the default is a space. If you pass an empty character, there is no separator
end
A string added to the end of the printed text, the default is a newline character '\n'
file
Specifies the file to which the text will be sent. Usually a file-like
write(string)
However, if file is used, the text to be printed will be output to the specified file instead of being printed on the screen.
|