gpt4 book ai didi

python - 在 Python 中指定项目分隔符

转载 作者:行者123 更新时间:2023-12-01 05:15:42 24 4
gpt4 key购买 nike

我是 Python 编程新手,使用的是 2.7.5 版本。我正在运行以下 python 语句

monthly_pay = 5000.0
annual_pay = 12*monthly_pay
print 'Your annual pay is $',format(annual_pay,',.2f')

当我运行它时,$ 符号和金额 60,000 之间有一个空格,我不想要这个空格,所以我尝试使用

monthly_pay = 5000.0
annual_pay = 12*monthly_pay
print ('Your annual pay is $',format(annual_pay,',.2f'), sep= ' ')

但在空闲时显示错误。我不明白为什么。有人可以帮忙吗?

我也尝试过类似的陈述print('One', 'Two', 'Three', sep='') 但再次如上所述,它显示了一个错误

最佳答案

您正在使用built-in function format(),但您可以通过使用字符串的 format() method 获得所需的结果。

print 'Your annual pay is ${}'.format(annual_pay,',.2f')

关于python - 在 Python 中指定项目分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23283764/

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