在python中打印变量和字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14041791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
print variable and a string in python
提问by user203558
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).
好的,我知道如何打印变量和字符串。但是我怎么能打印像“我的字符串”card.price(它是我的变量)之类的东西。我的意思是,这里是我的代码:
print "I have " (and here I would like to print my variable card.price)。
回答by Martijn Pieters
By printing multiple values separated by a comma:
通过打印以逗号分隔的多个值:
print "I have", card.price
The print statementwill output each expression separated by spaces, followed by a newline.
的打印语句将输出每一个表达分离用空格,跟着一个新行。
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.
或者使用较旧的和半弃用的%字符串格式化运算符。
回答by aemdy
Assuming you use Python 2.7 (not 3):
假设您使用 Python 2.7(不是 3):
print "I have", card.price(as mentioned above).
print "I have", card.price(正如刚才提到的)。
print "I have %s" % card.price(using string formatting)
print "I have %s" % card.price(使用字符串格式)
print " ".join(map(str, ["I have", card.price]))(by joining lists)
print " ".join(map(str, ["I have", card.price]))(通过加入列表)
There are a lot of ways to do the same, actually. I would prefer the second one.
实际上,有很多方法可以做到这一点。我更喜欢第二个。
回答by forresthopkinsa
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 开始,该%方法已被弃用。不要用那个。
回答by Kaye Louise
'''
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.
回答by Vignesh Krishnan
If you are using python 3.6 and newer then you can use f-strings to do the task like this.
如果您使用的是 python 3.6 和更新版本,那么您可以使用 f-strings 来完成这样的任务。
print(f"I have {card.price}")
just include f in front of your string and add the variable inside curly braces { }.
只需在字符串前面包含 f 并在花括号 { } 中添加变量。
Refer to a blog The new f-strings in Python 3.6: written by Christoph Zwerschke which includes execution times of the various method.
请参阅博客The new f-strings in Python 3.6:由 Christoph Zwerschke 编写,其中包括各种方法的执行时间。
回答by Daniel Gentile
From what I know, printing can be done in many ways
据我所知,打印可以通过多种方式完成
Here's what I follow:
这是我遵循的:
Printing string with variables
用变量打印字符串
a = 1
b = "ball"
print("I have", a, b)
Versus printing string with functions
与带函数的打印字符串对比
a = 1
b = "ball"
print("I have" + str(a) + str(b))
In this case, str() is a function that takes a variable and spits out what its assigned to as a string
在这种情况下, str() 是一个函数,它接受一个变量并将其分配给的内容作为字符串吐出
They both yield the same print, but in two different ways. I hope that was helpful
它们都产生相同的印刷品,但有两种不同的方式。我希望这有帮助

