在python 3.4中连接字符串和整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26090198/
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
Concatenate string and int in python 3.4
提问by apt-get
I'm new to Python, so I've been running through my own set of exercises to simply start memorizing basic functions and syntax. I'm using Pycharm IDE and Python 3.4. I've run into an issue when running through some basic str/int concatenation exercises. Each instance below is throwing an unsupported operand type. There are several threads on Stackoverflow that clearly states proper concatenation syntax, but the above error message continues to plague me.
我是 Python 新手,所以我一直在通过自己的一组练习来简单地开始记忆基本的函数和语法。我正在使用 Pycharm IDE 和 Python 3.4。在运行一些基本的 str/int 连接练习时,我遇到了一个问题。下面的每个实例都抛出一个不受支持的操作数类型。Stackoverflow 上有几个线程清楚地说明了正确的连接语法,但上述错误消息继续困扰着我。
print ("Type string: ") + str(123)
print ("Concatenate strings and ints "), 10
采纳答案by Carsten
Please remember (or if you don't know it yet, read up on the subject) that printis a function in Python 3. In Python 2, your first line would concatenate "Type string: " and "123" and then print them. In Python 3, you are calling the printfunction with one argument, which returns None, and then add "123" to it. That doesn't make any sense.
请记住(或者如果您还不知道,请阅读该主题)这print是 Python 3 中的一个函数。在 Python 2 中,您的第一行将连接“Type string:”和“123”,然后打印它们。在 Python 3 中,您print使用一个参数调用函数,该参数返回None,然后向其添加“123”。那没有任何意义。
The second line doesn't generate an error in Python 2 or 3 (I've tested it with 2.7.7 and 3.2.3). In Python 2, you get
第二行不会在 Python 2 或 3 中产生错误(我已经用 2.7.7 和 3.2.3 对其进行了测试)。在 Python 2 中,你得到
Concatenate strings and ints 10
连接字符串和整数 10
while in Python 3, your script should only print
而在 Python 3 中,你的脚本应该只打印
Concatenate strings and ints
连接字符串和整数
This is because again, print is a function, therefore you call it with the argument "Concatenate strings and ints". The , 10makes your line a tuple of the return value of print, which is None, and 10. Since you don't use that tuple for anything, there is no visible effect.
这是因为再次打印是一个函数,因此您使用参数“连接字符串和整数”来调用它。将, 10让你的行返回值的元组print,这是None和10。由于您不将该元组用于任何事情,因此没有明显的效果。
回答by gitaarik
回答by abarnert
What's wrong with this?
这有什么问题?
print ("Type string: ") + str(123)
printis just a function like anything else. And you're calling that function with one argument, "Type string: ", and then trying to add the result (which will be None) to the string '123'. That isn't going to work. If you wanted to add the two stringstogether, you have to put them into the same expression, inside the parens:
print只是一个像其他任何东西一样的函数。并且您使用一个参数调用该函数,"Type string: ",然后尝试将结果(将是None)添加到字符串'123'。那是行不通的。如果你想把这两个字符串加在一起,你必须把它们放在同一个表达式中,在括号内:
print("Type string: " + str(123))
Similarly:
相似地:
print ("Concatenate strings and ints "), 10
This calls printwith one argument, and then makes a tuple of the Nonereturned by printand the number 10. If you want to pass 10to the printcall, it has to go inside the parens:
这print用一个参数调用,然后生成一个由None返回的 byprint和数字 10组成的元组。如果你想传递10给print调用,它必须进入括号内:
print("Concatenate strings and ints ", 10)
As rednaw's answer points out, using str.formatis more flexible, and avoids the possibility of problems like this. It also gives you code that works exactly the same way in both Python 2.6-2.7 and Python 3.x, which is pretty nice even if you aren't trying to write dual-platform/single-codebase code, because it'll be understandable even to people who only know one or the other.
正如rednaw的回答所指出的那样,使用str.format更加灵活,避免了出现此类问题的可能性。它还为您提供了在 Python 2.6-2.7 和 Python 3.x 中工作方式完全相同的代码,即使您不尝试编写双平台/单代码库代码,这也非常好,因为它将即使是只知道其中之一的人也能理解。
回答by Rafael Lerm
In Python 3+, printis a function, so it must be called with its arguments between parentheses. So looking at your example:
在 Python 3+ 中,print是一个函数,因此必须在括号中使用其参数调用它。所以看看你的例子:
print ("Type string: ") + str(123)
It's actually the same as:
它实际上与以下相同:
var = print("Type string: ")
var + str(123)
Since printreturns nothing (in Python, this means None), this is the equivalent of:
由于不print返回任何内容(在 Python 中,这意味着None),这相当于:
None + str(123)
which evidently will give an error.
这显然会出错。
That being said about what you tried to do, what you wantdo to is very easy: pass the printfunctionwhat you mean to print, which can be done in various ways:
话虽如此,关于您尝试做什么,您想要做什么非常简单:传递您要打印的print功能,这可以通过多种方式完成:
print ("Type string: " + str(123))
# Using format method to generate a string with the desired contents
print ("Type string: {}".format(123))
# Using Python3's implicit concatenation of its arguments, does not work the same in Python2:
print ("Type string:", str(123)) # Notice this will insert a space between the parameters
回答by SnowBG
You can do it like this:
你可以这样做:
c = 'Emerson'
d = 32
print("My name is %s and I am %d years old." %(c,d))
Result:
结果:
My name is Emerson and I am 32 years old.
我叫爱默生,今年 32 岁。
回答by sayalok
i think this is pretty cool way to Concatenate string and int in python
我认为这是在 python 中连接 string 和 int 的非常酷的方法
print (f"Type string: {123}")
print (f"Concatenate strings and ints {10}")

