gpt4 book ai didi

python - 在 Python 中连接字符串和整数

转载 作者:IT老高 更新时间:2023-10-28 12:34:50 26 4
gpt4 key购买 nike

在 Python 中说你有

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

会给你错误,所以你写

print s + str(i)

不会出错。

我认为这是处理 int 和字符串连接的一种非常笨拙的方法。

即使是 Java 也不需要显式转换为 String 来执行这种连接。有没有更好的方法来进行这种连接,即在 Python 中不进行显式转换?

最佳答案

现代字符串格式:

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

关于python - 在 Python 中连接字符串和整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11559062/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com