在python中连接字符串和整数

时间:2021-06-19 00:20:21

In python say you have

在python中说你有

s = "string"
i = 0
print s+i

will give you error so you write

会给你错误,所以你写

print s+str(i) 

to not get error.

没有得到错误。

I think this is quite a clumsy way to handle int and string concatenation. Even Java does not need explicit casting to String to do this sort of concatenation. Is there a better way to do this sort of concatenation i.e without explicit casting in Python?

我认为这是处理int和字符串连接的一种笨拙的方式。甚至Java也不需要显式转换为String来进行这种连接。是否有更好的方法来进行这种连接,即在Python中没有显式转换?

4 个解决方案

#1


117  

Modern string formatting:

现代字符串格式:

"{} and {}".format("string", 1)

#2


72  

No string formatting:

没有字符串格式:

>> print 'Foo',0
Foo 0

#3


24  

String formatting, using the new-style .format() method (with the defaults .format() provides):

字符串格式化,使用新式的.format()方法(默认为.format()提供):

 '{}{}'.format(s, i)

Or the older, but "still sticking around", %-formatting:

或者更老,但“仍然坚持”,% - 格式:

 '%s%d' %(s, i)

In both examples above there's no space between the two items concatenated. If space is needed, it can simply be added in the format strings.

在上面的两个例子中,连接的两个项目之间没有空格。如果需要空间,可以简单地将其添加到格式字符串中。

These provide a lot of control and flexibility about how to concatenate items, the space between them etc. For details about format specifications see this.

这些为如何连接项目,它们之间的空间等提供了很多控制和灵活性。有关格式规范的详细信息,请参阅此内容。

#4


11  

Python is an interesting language in that while there is usually one (or two) "obvious" ways to accomplish any given task, flexibility still exists.

Python是一种有趣的语言,虽然通常有一种(或两种)“明显”的方法来完成任何给定的任务,但灵活性仍然存在。

s = "string"
i = 0

print (s + repr(i))

The above code snippet is written in Python3 syntax but the parentheses after print were always allowed (optional) until version 3 made them mandatory.

上面的代码片段是用Python3语法编写的,但是打印后的括号始终是允许的(可选),直到版本3使它们成为必需的。

Hope this helps.

希望这可以帮助。

Caitlin

凯特琳

#1


117  

Modern string formatting:

现代字符串格式:

"{} and {}".format("string", 1)

#2


72  

No string formatting:

没有字符串格式:

>> print 'Foo',0
Foo 0

#3


24  

String formatting, using the new-style .format() method (with the defaults .format() provides):

字符串格式化,使用新式的.format()方法(默认为.format()提供):

 '{}{}'.format(s, i)

Or the older, but "still sticking around", %-formatting:

或者更老,但“仍然坚持”,% - 格式:

 '%s%d' %(s, i)

In both examples above there's no space between the two items concatenated. If space is needed, it can simply be added in the format strings.

在上面的两个例子中,连接的两个项目之间没有空格。如果需要空间,可以简单地将其添加到格式字符串中。

These provide a lot of control and flexibility about how to concatenate items, the space between them etc. For details about format specifications see this.

这些为如何连接项目,它们之间的空间等提供了很多控制和灵活性。有关格式规范的详细信息,请参阅此内容。

#4


11  

Python is an interesting language in that while there is usually one (or two) "obvious" ways to accomplish any given task, flexibility still exists.

Python是一种有趣的语言,虽然通常有一种(或两种)“明显”的方法来完成任何给定的任务,但灵活性仍然存在。

s = "string"
i = 0

print (s + repr(i))

The above code snippet is written in Python3 syntax but the parentheses after print were always allowed (optional) until version 3 made them mandatory.

上面的代码片段是用Python3语法编写的,但是打印后的括号始终是允许的(可选),直到版本3使它们成为必需的。

Hope this helps.

希望这可以帮助。

Caitlin

凯特琳