在python 2中,python print语句不是函数,而在python 3中,这已经变成了一个函数
当我输入print(我得到一些hovertext (或类似的东西)来
print(value,...,sep=' ', end='\n', file=sys.stdout, flush=False)
Run Code Online (Sandbox Code Playgroud)
我知道什么是值意味着但是澄清了那些其他变量意味着什么以及python 3的print语句优于python 2的优点是什么 (especially sep=' ')
当你提供多个参数时,print它们通常用空格分隔:
>>> print(1, 2, 3)
1 2 3
Run Code Online (Sandbox Code Playgroud)
sep 允许您将其更改为其他内容:
>>> print(1, 2, 3, sep=', ')
1, 2, 3
Run Code Online (Sandbox Code Playgroud)
通常,print会在末尾添加一个新行.end让你改变:
>>> print('Hello.', end='')
Hello.>>>
Run Code Online (Sandbox Code Playgroud)
通常print会写入标准输出.file让你改变:
>>> with open('test.txt', 'w') as f:
... print("Hello, world!", file=f)
...
Run Code Online (Sandbox Code Playgroud)
通常print不会显式刷新流.如果你想避免额外的sys.stdout.flush(),你可以使用flush.通常很难看到这种效果,但尝试这一点不flush=True应该使它可见:
>>> import time
>>> while True:
... print('.', end='', flush=True)
... time.sleep(0.5)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
662 次 |
| 最近记录: |