在python中打印变量和字符串 [英] print variable and a string in python
问题描述
好的,我知道如何打印变量和字符串.但是我怎么能打印像我的字符串"card.price(它是我的变量)之类的东西.我的意思是,这是我的代码:打印我有"(在这里我想打印我的变量card.price)
.
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 "I have " (and here I would like to print my variable card.price)
.
推荐答案
通过打印以逗号分隔的多个值:
By printing multiple values separated by a comma:
print "I have", card.price
print 语句 将输出以空格分隔的每个表达式,后跟换行.
The print statement will output each expression separated by spaces, followed by a newline.
如果您需要更复杂的格式,请使用 ''.format()
方法:
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.
这篇关于在python中打印变量和字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!