我是使用3.7的Python的新手,我试图在整数变量前面添加“ $”。在执行了许多搜索之后,似乎它们有很多格式化字符串的方法。我的目标是在print()中添加一个“ $”。
我尝试过print('Total amount is' , houseAmount , ' of the house' , sep = ' $')
。但没有运气。
代码:
houseAmount = float(300000)
hoa = float(550.4)
print('House amount is' , houseAmount , ' for the house.' , 'HOA amount for the house is' , hoa , 'every year') (I would like to add a "$" before "houseAmount" and "hoa")
结果:
House amount is $300000 for the house. HOA amount for the house is $550.4 every year
答案 0 :(得分:1)
print("House amount is ${} for the house. HOA amount for the house is ${} every year".format(houseAmount, hoa))
请记住,它们是按顺序执行的。