1、今天樓主第N遍復(fù)習(xí)Python的時候票灰,看到了這個:
2女阀、唉?屑迂?浸策?"\t"
是類似于空格效果的制表符,這個樓主了解惹盼,但是后面那個end=‘’
是什么鬼庸汗?
3、查了一下print語句的說明文檔逻锐,end里的值是跟在輸出值后面的小尾巴夫晌,默認是“\n”
也就是換行。
def print(*args, **kwargs): # known special case of print
"""
print(value, ..., sep=' ', end='\n', file=sys.stdout)
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.
"""
pass```
4昧诱、光說不練咋行晓淀?來兩下。
from future import print_function
print('hello1')
print('hello2', end="\n")
print('hello3')
print('hello4', end="") #這是空字符串
print('hello5')
print('hello6', end=" ") #這是空格
print('hello7')```
5盏档、唉凶掰??蜈亩?第一行那個是什么懦窘,那是因為樓主用的是Python 2.7,而這個貌似在Python 3以后的版本中才有稚配,所以樓主從未來的版本中“借”一下print新功能畅涂。
6、唉道川?午衰?立宜?print()還有個sep參數(shù)貌似是各個值之間的連接符,默認是空格臊岸。走橙数,走兩步。
from __future__ import print_function
print('hello', 'hello', 'hello')
print('hello', 'hello', 'hello', sep=" ")
print('hello', 'hello', 'hello', sep=" ")
print('hello', 'hello', 'hello', sep="+")
print('hello', 'hello', 'hello', sep="-")
print('hello', 'hello', 'hello', sep="+-*/")```
![輸出結(jié)果](http://upload-images.jianshu.io/upload_images/2164666-09230f5948cbf764.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
7帅戒、唉灯帮??逻住?print()還有個file參數(shù)干哈的钟哥?file參數(shù)就是把print的結(jié)果輸出到哪里去的作用。默認是系統(tǒng)標準輸出鄙信,就是在交互shell里輸出瞪醋。
8、唉装诡?银受??那是不是可以輸出到一個文件里把徊伞宾巍?額……好吧,試試渔伯。
from future import print_function
with open('print_output.txt','w') as f:
print('hello', 'hello', 'hello', sep="+-*/", file=f)```
9顶霞、唉?锣吼?选浑?貌似還有個flush參——
10、再見玄叠!