python sep参数_Python中带有print()函数的sep参数

sep参数代表分隔符,它与print()函数一起使用以指定参数之间的分隔符。

默认值为空格,即,如果我们不使用sep参数,则参数的值由空格分隔。使用“ sep”,我们可以使用任何字符,整数或字符串。

注意: “ sep”在Python3.x或更高版本中可用。

语法:

print(argument1, argument2, ..., sep = value)

带有'sep'参数的Python示例 print()

范例1:

# 变数

name = "Mike"

age = 21

city = "Washington, D.C."

# 不使用sep参数进行打印

print("不使用sep参数...")

print(name, age, city)

print()

# 使用sep参数进行打印

# 用空格隔开

print("With using sep parameter (separated by spaces)")

print(name, age, city, sep=' ')

print()

# 使用sep参数进行打印

# 以冒号(:)分隔

print("With using sep parameter (separated by colon)")

print(name, age, city, sep=':')

print()

# 使用sep参数进行打印

# separated by " -> "

print("With using sep parameter (separated by ' -> ')")

print(name, age, city, sep=' -> ')

print()

输出:

不使用sep参数...

Mike 21 Washington, D.C.

With using sep parameter (separated by spaces)

Mike 21 Washington, D.C.

With using sep parameter (separated by colon)

Mike:21:Washington, D.C.

With using sep parameter (separated by ' -> ')

Mike -> 21 -> Washington, D.C.

范例2:

# 变数

name = "Mike"

age = 21

city = "Washington, D.C."

# 使用sep参数进行打印

# 禁用空格分隔符

print("With using sep parameter (disable space separator)")

print(name, age, city, sep='')

print()

# 使用sep参数进行打印

# 以数字分隔

print("With using sep parameter (separated by number)")

print(name, age, city, sep='12345')

print()

# 使用sep参数进行打印

# separated by " ### "

print("With using sep parameter (separated by ' ### ')")

print(name, age, city, sep=' ### ')

print()

输出:

With using sep parameter (disable space separator)

Mike21Washington, D.C.

With using sep parameter (separated by number)

Mike123452112345Washington, D.C.

With using sep parameter (separated by ' ### ')

Mike ### 21 ### 华盛顿特区

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值