Python 中 sep 和 end 参数详解:print 函数格式控制
在 Python 中,'sep' 和 'end' 是两个关键字参数,用于控制 'print' 函数输出内容的格式。
- 'sep' 参数
'sep' 参数用于指定输出多个参数时的分隔符,默认为一个空格。
例如:
print('a', 'b', 'c') # 输出 a b c
print('a', 'b', 'c', sep=',') # 输出 a,b,c
print('a', 'b', 'c', sep='-') # 输出 a-b-c
- 'end' 参数
'end' 参数用于指定输出结束时的字符,默认为一个换行符。
例如:
print('hello', end='') # 输出 hello,但不换行
print('world') # 输出 world,换行
print('hello', end=' ') # 输出 hello,但不换行,以空格结尾
print('world') # 输出 world,换行
综合使用 'sep' 和 'end' 参数:
print('a', 'b', 'c', sep=',', end='') # 输出 a,b,c,但不换行
print('d', 'e', 'f', sep='-') # 输出 d-e-f,并换行
输出结果为:
a,b,cd-e-f
原文地址: https://www.cveoy.top/t/topic/otU5 著作权归作者所有。请勿转载和采集!