我试图创建一个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美元。
尝试内联格式化
print(f"After {yr} year(s) your balance will be ${compute_compound_interest(ibal, ir, yr)}.")
是:
print('A','B','C',sep='')
输出:
ABC
请注意,您需要在需要的位置添加空格,例如:
x = 1
print('After',x,'year',sep='') #prints After1year
print('After ',x,' year',sep='') #prints After 1 year
Daweo所说的是完全正确的,但你也可以看看python格式的字符串,这会使你的代码看起来像这样:
print("After {} year(s) your balance will be ${}.".format(yr, compute_compound_interest(ibal, ir, yr)))