在python中连接字符串和整数 [英] Concatenating string and integer in python

查看:127
本文介绍了在python中连接字符串和整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中说你有

In python say you have

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

会给你错误
,所以你写

will give you error so you write

print s+str(i) 

不会出错。

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

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?

推荐答案

现代字符串格式:

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

这篇关于在python中连接字符串和整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆