1、今天楼主第N遍复习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参数貌似是各个值之间的连接符,默认是空格。走,走两步。
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="+-*/")```
![输出结果](http://upload-images.jianshu.io/upload_images/2164666-09230f5948cbf764.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
7、唉???print()还有个file参数干哈的?file参数就是把print的结果输出到哪里去的作用。默认是系统标准输出,就是在交互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、再见!