【问题标题】:Adding $ infront of variable within print statement in python 3.3.4在 python 3.3.4 的 print 语句中添加 $ infront 变量
【发布时间】:2014-03-18 07:44:33
【问题描述】:

我有如下声明:

print ("You have entered the price of ",var1, "for a chocolate bar")

这很有效,但是我需要在它前面放一个“$”来输出。我尝试将它隐藏在 print 语句中,但是你最终会在它之间产生一个差距,所以你最终会得到

'You have entered the price of $ 34.33 for a chocolate bar'.

我需要它:

'You have entered the price of $34.33 for a chocolate bar'.

我对 python 很陌生,这让我很困惑。 任何帮助,将不胜感激。我正在使用 python 3.3.4

【问题讨论】:

    标签: python


    【解决方案1】:

    format 是推荐的方法。

    print("You have entered the price of ${0} for a chocolate bar".format(var1))
    

    【讨论】:

    • 我在哪里可以找到有关格式是推荐方式这一事实的文档?
    【解决方案2】:
    print "You have entered the price of ","$"+str(var1), "for a chocolate bar"
    

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 1970-01-01
      • 2021-10-28
      • 2015-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多