攜手創作,共同成長!這是我參與「掘金日新計劃 · 8 月更文挑戰」的第 6 天,點擊查看活動詳情
Python print() 函數The message is printed to the screen or any other standard output device.
語法: print(value(s), sep= ' ', end = '\n', file=file, flush=flush)
參數:
- value(s) : 任何值,只要你喜歡.Before printing will be converted to a string
- sep='separator' :( 可選)Specify how to separate the object,如果有多個.默認值:' '
- end='end':( 可選)The last to print the contents of the specified.默認值:'\n'
- file :( 可選)具有 write 方法的對象.默認:sys.stdout
- flush :( 可選)布爾值,Specify the output is refresh(True)Or the buffer(False).默認值:假
返回: It will be output to return to the screen.
Although you don't need to print() 函數中傳遞參數,But it needs to be in the end there is a empty parentheses,告訴 python Executive function rather than to call it by name.現在,Let's explore can be used to print() 函數的可選參數.
python 的 print Statements in the string literal is mainly used for formatting, design or a particular string in the use of print() When printing function display mode.
例子:
print("juejin \n is best for DSA Content.")
復制代碼
輸出:
juejin
is best for DSA Content.
復制代碼
end The keyword is used to specify the in print() At the end of the function to print the contents of the.默認設置為“\n”,這會導致 print() After the statement execution line.
# The trip will be before the next print statements automatically add a new row
print ("juejin is the best platform for DSA content")
# 此 print() The last set of functions to“**”結尾argument.
print ("juejin is the best platform for DSA content", end= "**")
print("Welcome to JJ")
復制代碼
輸出:
juejin is the best platform for DSA content
juejin is the best platform for DSA content**Welcome to JJ
復制代碼
python 中的 I/O Usually the buffer,This means that they use in the form of block.這就是flush的用武之地,Because it can help users decide whether you need the contents of the buffer to write.默認情況下,它設置為 false.如果設置為 true,The output will be written as one after another character sequence.這個過程很慢,Because it's easier to block write instead of a written one character at a time.為了理解 print() 函數中 flush Parameters of the case,我們舉個例子.
例子:
想象一下,You are building a countdown timer,It a second time remaining attached to the same line.它看起來像下面這樣:
3>>>2>>>1>>>Start
復制代碼
The initial code is as follows;
import time
count_seconds = 3
for i in reversed(range(count_seconds + 1)):
if i > 0:
print(i, end='>>>')
time.sleep(1)
else:
print('Start')
復制代碼
因此,The above code added no trailing newline character text,And then after each add text dormancy for a second.At the end of the countdown,它會打印 Start And an end to the bank.If you run the code according to the sample,它會等待 3 Seconds and suddenly to print the whole text.This is due to the buffer of the text block 3 The waste seconds,如下所示:
Although the buffer with a purpose,But it may cause shown above the ill effects of.In order to solve the same problem,flush 參數與 print() 函數一起使用.現在,將 flush 參數設置為 true And once again to see the results.
import time
count_seconds = 3
for i in reversed(range(count_seconds + 1)):
if i > 0:
print(i, end='>>>', flush = True)
time.sleep(1)
else:
print('Start')
復制代碼
輸出:
3>>>2>>>1>>>Start
復制代碼
print() Functions can take any number of positional parameters.這些參數可以使用 “,”A delimiter separated each other.These are mainly used for in a single print() Function of formatting multiple statements.
例子:
b = "for"
print("Haiyong", b , "Code")
復制代碼
輸出:
Haiyong for Code
復制代碼
與流行的看法相反,print() Don't () function converts a message into the text on the screen.These are done by low-level code layer,Data can be read in bytes(消息).print() Function is the level of an interface,It will actually print entrusted to flow or類似文件的對象.默認情況下,print() 函數通過 file 參數 綁定到sys.stdout .
import io
# Declare a virtual file
dummy_file = io.StringIO()
# The message is added to the virtual file
print('Hello Code!!', file=dummy_file)
# Get the value from the virtual file
dummy_file.getvalue()
復制代碼
輸出:
'Hello Code!!\n'
復制代碼
# Shows how to print data on the screen Python 3.x 程序
# 一個對象通過
print("Haiyong")
x = 5
# 傳遞了兩個對象
print("x =", x)
# Disable the soft space function code
print('H', 'Y', 'G', sep='')
# With the end parameters
print("Python", end='@')
print("Haiyong")
復制代碼
輸出:
Haiyong
x = 5
HYG
[email protected]
復制代碼