在Python中,sep和end是两个关键字参数,用于控制print函数输出内容的格式。

  1. 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
  1. 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
``
Python中sep和end区别举例说明

原文地址: https://www.cveoy.top/t/topic/gclD 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录