简体   繁体   English

(' + str(i) + ') 背后的逻辑是什么?

[英]What is the logic behind (' + str(i) + ')?

I am new to programming, and stamp upon this line:我是编程新手,并在此行上加盖:

print('My name is')
for i in range(5):
   print('Jimmy Five Times (' + str(i) + ')')

I don't understand this part (' + str(i) + ')'), to be more specific, the + sign before and after str(i) [especially the + sign after str(i)].我不明白这部分 (' + str(i) + ')'),更具体地说,str(i) 前后的 + 号 [尤其是 str(i) 之后的 + 号]。

I know the + sign is for strings concatenation, But in the above line, what is combined with what?我知道 + 符号用于字符串连接,但在上面的行中,什么与什么结合?

And what is the difference between the first example and say:第一个例子和说有什么区别:

print('My name is')
for i in range(5):
    print('Jimmy five times ' + str(i))

Can someone give an example for a similar but complex example for such use?有人可以举一个类似但复杂的例子来说明这种用途吗?

Thanks.谢谢。

edit: I realized that the string is Jimmy Five Times ( ,not Jimmy Five Times , that left and right brackets confused me. Thanks all for your quick responses.编辑:我意识到字符串是吉米五次(不是吉米五次,左右括号让我感到困惑。感谢大家的快速回复。

+ concatenates strings, or if operands are numbers then adds them. +连接字符串,或者如果操作数是数字,则将它们相加。 Here you need to do str(i) because if i is not a string, then python tries to add them as integers, which is not possible.这里你需要做str(i)因为如果i不是字符串,那么 python 会尝试将它们添加为整数,这是不可能的。 And the ending + ')' will add a closing parentheses to the string, and second example does not add parentheses.结尾的+ ')'会为字符串添加一个右括号,第二个示例不添加括号。 See yourself, first one will:看看你自己,第一个会:

My name is
Jimmy Five Times (0)
Jimmy Five Times (1)
Jimmy Five Times (2)
Jimmy Five Times (3)
Jimmy Five Times (4)

And second one:第二个:

My name is
Jimmy five times 0
Jimmy five times 1
Jimmy five times 2
Jimmy five times 3
Jimmy five times 4

The sign + in the context of strings is used for string concatenation.字符串上下文中的符号+用于字符串连接。

Specifically, in this example, the + operand is not defined for string and int ( i is an integer) so you first have to convert it to string by str(i) and then you can contact the strings.具体来说,在这个例子中, +操作数没有为 string 和 int 定义( i是一个整数),所以你首先必须通过str(i)将它转换为字符串,然后你可以联系字符串。

For the first code mentioned, the first string 'Jimmy Five Times (' is concatenated with str(i) and then this str(i) is further concatenated with ')' so the final output is :对于提到的第一个代码,第一个字符串 'Jimmy Five Times (' 与str(i) ,然后此str(i)进一步与 ')' 连接,因此最终输出为:

Jimmy Five Times (0)
Jimmy Five Times (1)
Jimmy Five Times (2)
Jimmy Five Times (3)
Jimmy Five Times (4)

Explanation解释

The sign + in the context of strings is used for string concatenation.字符串上下文中的符号 + 用于字符串连接。

Specifically, in this example, the + operand is not defined for string and int (i is an integer) so you first have to convert it to string by str(i) and then you can contact the strings.具体来说,在这个例子中,+ 操作数没有为 string 和 int 定义(i 是一个整数),所以你首先必须通过str(i)将它转换为字符串,然后你可以联系字符串。

Output输出

First one's output:第一个输出:

My name is
Jimmy Five Times (0)
Jimmy Five Times (1)
Jimmy Five Times (2)
Jimmy Five Times (3)
Jimmy Five Times (4)

And second one's output:第二个输出:

My name is
Jimmy five times 0
Jimmy five times 1
Jimmy five times 2
Jimmy five times 3
Jimmy five times 4

Better solutions更好的解决方案

using f' strings python 3.6 up使用f'字符串 python 3.6 up

print(f'Jimmy Five Times ({i})')

using .format使用.format

print('My name is')
for i in range(5):
   print('Jimmy Five Times ({0})'.format(i))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM