gpt4 book ai didi

python打印格式和换行

转载 作者:行者123 更新时间:2023-12-01 02:30:31 25 4
gpt4 key购买 nike

我有这个循环:

for n in range(101):
money_growth = A*(1 + p/100)**n
print "%d Euro after %3d year[s] with \
interest of %0.2f will be %0.2f" %(A, n, p, money_growth)

我在 print 语句前面用\换行,但由于循环意图,它会额外的制表符:

00 year[s] with     interest of 0.05

我发现的唯一解决方案是这样做:

    for n in range(101):
money_growth = A*(1 + p/100)**n
print "%d Euro after %3d year[s] with \
interest of %0.2f will be %0.2f" %(A, n, p, money_growth)

但它以某种方式破坏了可读性还有更好的办法吗?

最佳答案

您可以通过将字符串分解为以下方式来使用隐式字符串连接:

for n in range(101):
money_growth = A*(1 + p/100)**n
print "%d Euro after %3d year[s] with" \
"interest of %0.2f will be %0.2f" %(A, n, p, money_growth)

关于python打印格式和换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46871260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com