在python中打印变量和字符串

时间:2021-10-08 23:07:37

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).

我知道如何打印变量和字符串。但是我怎么打印“我的字符串”卡呢?价格(它是我的变量)。我的意思是,这是我的代码:打印“我有”(这里我想打印可变卡。price)。

4 个解决方案

#1


30  

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.

print语句将输出由空格分隔的每个表达式,后跟一个换行符。

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

如果需要更复杂的格式,请使用“.format()方法:

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

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

或者使用旧的和半废弃的% string格式操作符。

#2


10  

Something that (surprisingly) hasn't been mentioned here is simple concatenation.

这里没有提到的(令人惊讶的)是简单的连接。

Example:

例子:

foo = "seven"

print("She lives with " + foo + " small men")

Result:

结果:

She lives with seven small men

她和七个小男人住在一起。

Additionally, as of Python 3, the % method is deprecated. Don't use that.

此外,在Python 3中,%方法被弃用。不要使用。

#3


8  

Assuming you use Python 2.7 (not 3):

假设您使用的是Python 2.7(不是3):

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

打印“我有”,卡。价格(如上所述)。

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

打印“我有%s”%卡。价格(使用字符串格式化)

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

打印”。join(map(str, ["I have", card.price])(通过加入列表)

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

实际上,有很多方法可以做到这一点。我想要第二个。

#4


1  

'''

If the python version you installed is 3.6.1, you can print strings and a variable through
a single line of code.
For example the first string is "I have", the second string is "US
Dollars" and the variable, **card.price** is equal to 300, we can write
the code this way:

'''

print("I have", card.price, "US Dollars")

#The print() function outputs strings to the screen.  
#The comma lets you concatenate and print strings and variables together in a single line of code.

#1


30  

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.

print语句将输出由空格分隔的每个表达式,后跟一个换行符。

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

如果需要更复杂的格式,请使用“.format()方法:

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

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

或者使用旧的和半废弃的% string格式操作符。

#2


10  

Something that (surprisingly) hasn't been mentioned here is simple concatenation.

这里没有提到的(令人惊讶的)是简单的连接。

Example:

例子:

foo = "seven"

print("She lives with " + foo + " small men")

Result:

结果:

She lives with seven small men

她和七个小男人住在一起。

Additionally, as of Python 3, the % method is deprecated. Don't use that.

此外,在Python 3中,%方法被弃用。不要使用。

#3


8  

Assuming you use Python 2.7 (not 3):

假设您使用的是Python 2.7(不是3):

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

打印“我有”,卡。价格(如上所述)。

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

打印“我有%s”%卡。价格(使用字符串格式化)

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

打印”。join(map(str, ["I have", card.price])(通过加入列表)

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

实际上,有很多方法可以做到这一点。我想要第二个。

#4


1  

'''

If the python version you installed is 3.6.1, you can print strings and a variable through
a single line of code.
For example the first string is "I have", the second string is "US
Dollars" and the variable, **card.price** is equal to 300, we can write
the code this way:

'''

print("I have", card.price, "US Dollars")

#The print() function outputs strings to the screen.  
#The comma lets you concatenate and print strings and variables together in a single line of code.