print variable and a string in python

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

    If you are using python 3.6 and newer then you can use f-strings to do the task like this.

    print(f"I have {card.price}")
    

    just include f in front of your string and add the variable inside curly braces { }.

    Refer to a blog The new f-strings in Python 3.6: written by Christoph Zwerschke which includes execution times of the various method.

提交回复
热议问题