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

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (33)

我是使用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
提问于
用户回答回答于
print("House amount is ${} for the house. HOA amount for the house is ${} every year".format(houseAmount, hoa))

请记住它按顺序执行这些操作。

扫码关注云+社区

领取腾讯云代金券