Python3中的print函数

Python3中的输出语句:

函数原型如下:
print(help(print))  使用此语句打印


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.

sep

分割符,默认为空格

end

结尾的结束符,默认为新行

file

输出流,默认为标准输出流

flush

是否刷新输出缓冲区

分隔符:

a = 'hello'
b = 'Python'
c = 'ixusy88'

# 默认空格
print('1----',a,b,c)

# 指定分割符
print('2----',a,b,c,sep='!')

print('3----',a,b,c,sep=',')

"""
输出结果:
1---- hello Python ixusy88
2----!hello!Python!ixusy88
3----,hello,Python,ixusy88
"""

end:

a = 'hello'
b = 'Python'
c = 'ixusy88'

# end 默认是新的一行,即下一个打印会在新的一行输出
print('1----',a,b,c)

# 指定结束符,
print('2----',a,b,c,end='###')
print('3----',a,b,c)
print('4----',a,b,c)

"""
结果:
1---- hello Python ixusy88
2---- hello Python ixusy88###3---- hello Python ixusy88
4---- hello Python ixusy88
"""

输出流:

a = 'hello'
b = 'Python'
c = 'ixusy88'

# file ,默认是sys.stdout
print('1----',a,b,c)

# file,指定输出流,输出到文件中
print('2----',a,b,c,file=open('test.txt','w'))
print('-1--')
# 读取文件,并打印出来
print(open('test.txt').read() )
print('-2--')

# 也可以修改sys.stdout,把定向到一个文件,之后的所有输出都会保存到文件中;
import sys
print('sys.stdout之后')
sys.stdout = open('test_2.txt','w')
print('1----',a,b,c)
print(sys.platform)


"""
输出结果:
1---- hello Python ixusy88
-1--
2---- hello Python ixusy88

-2--
sys.stdout之后
"""

输出文件中的内容

 

 

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值