删除python打印空格

问题描述 投票:0回答:3

我试图创建一个python print语句,调用其中的函数并将值返回到print语句,但因为python每次有逗号时都会添加空格,格式看起来很奇怪。有没有办法强制python不添加它在print语句中的空格?

print("After", yr, "year(s) your balance will be $", compute_compound_interest(ibal, ir, yr),'.')

这是打印出来的:

1年后,您的余额将为110.0美元。

我希望它看起来像:

1年后,您的余额将为110.00美元。

python python-3.x printing formatting
3个回答
0
投票

尝试内联格式化

print(f"After {yr} year(s) your balance will be ${compute_compound_interest(ibal, ir, yr)}.")

文档:https://www.python.org/dev/peps/pep-0498/


0
投票

是:

print('A','B','C',sep='')

输出:

ABC

请注意,您需要在需要的位置添加空格,例如:

x = 1
print('After',x,'year',sep='') #prints After1year
print('After ',x,' year',sep='') #prints After 1 year

0
投票

Daweo所说的是完全正确的,但你也可以看看python格式的字符串,这会使你的代码看起来像这样:


print("After {} year(s) your balance will be ${}.".format(yr, compute_compound_interest(ibal, ir, yr)))
© www.soinside.com 2019 - 2024. All rights reserved.