Python3 print()函数sep,end,file参数用法练习
来自builtins.py:
def print(self, *args, sep=' ', end='\n', file=None): # known special case of print
"""
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
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.
flush: whether to forcibly flush the stream.
"""
pass
使用1,练习sep的用法:
print("file\n","abc","bcd","fff\n","poi") print("-------------") print("file\n","abc","bcd","fff\n","poi",sep='') print("-------------") print("file\n","abc","bcd","fff\n","poi",sep=' ') print("-------------")
其结果为:
file abc bcd fff poi ------------- file abcbcdfff poi ------------- file abc bcd fff poi -------------
使用2,练习end的用法:
1 print("file\n","abc","fff",sep='#########\n',end='') 2 print("--------------------") 3 print("file\n","abc","fff",sep='#########\n',end='\n') 4 print("--------------------") 5 print("file\n","abc","fff",sep='#########\n') 6 print("--------------------")
其结果为:
file ######### abc######### fff-------------------- file ######### abc######### fff -------------------- file ######### abc######### fff --------------------
使用3,练习file的用法:
1 with open('abc.txt','w') as f: 2 print("file\n","abc","fff",sep='#########\n',end='',file=f)
其结果:
将输出写到了文件abc.txt。
abc.txt的内容如下:
file ######### abc######### fff
注意文件末行没有换行符
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南