python常见的输出格式总结

1. 字符串插值/格式化字符串字面值 (f-string, Python 3.6及更高版本)

name = "World"
print(f"Hello, {name}!")

2. str.format() 方法

name = "World"
print("Hello, {}!".format(name))

或者,使用位置参数和关键字参数:

print("Hello, {0}! This is {name}.".format("World", name="Python"))

3. % 运算符

name = "World"
print("Hello, %s!" % name)

对于多个变量,可以使用元组:

x = 10
y = 20
print("x = %d, y = %d" % (x, y))

4. 模板字符串

from string import Template
name = "World"
t = Template('Hello, $name!')
print(t.substitute(name=name))

5. 使用 print() 函数的 sepend 参数

print("Hello", "World", sep=", ", end="!\n")

其中 sep 参数定义了不同项之间的分隔符,end 参数定义了字符串的结束字符。

6. 多行字符串

name = "World"
print(f"""
Hello,
{name}!
""")
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值