如何使用print()在变量前面添加字符

时间:2019-06-09 16:30:42

标签: python-3.x

我是使用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

1 个答案:

答案 0 :(得分:1)

print("House amount is ${} for the house. HOA amount for the house is ${} every year".format(houseAmount, hoa))

请记住,它们是按顺序执行的。