print variable and a string in python

前端 未结 7 2127
逝去的感伤
逝去的感伤 2020-12-07 23:49

Alright, I know how to print variables and strings. But how can I print something like \"My string\" card.price (it is my variable). I mean, here is my code: print \"

7条回答
  •  感动是毒
    2020-12-08 00:43

    Assuming you use Python 2.7 (not 3):

    print "I have", card.price (as mentioned above).

    print "I have %s" % card.price (using string formatting)

    print " ".join(map(str, ["I have", card.price])) (by joining lists)

    There are a lot of ways to do the same, actually. I would prefer the second one.

提交回复
热议问题