print variable and a string in python

前端 未结 7 2074
逝去的感伤
逝去的感伤 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:37

    By printing multiple values separated by a comma:

    print "I have", card.price
    

    The print statement will output each expression separated by spaces, followed by a newline.

    If you need more complex formatting, use the ''.format() method:

    print "I have: {0.price}".format(card)
    

    or by using the older and semi-deprecated % string formatting operator.

提交回复
热议问题