gpt4 book ai didi

python - 类型错误 : can only concatenate str (not "int") to str What does this mean?

转载 作者:行者123 更新时间:2023-12-01 22:50:03 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Python: TypeError: cannot concatenate 'str' and 'int' objects [duplicate]

(9 个回答)


3年前关闭。



def myFunc(King):
print("The characters name is " + King.name + " and age is " + King.age)

如何将 int 链接到字符串?

最佳答案

这正是错误消息所说的:"The characters name is " + King.name + " and age is "是一个字符串。您不能向其添加整数 ( King.age )。

幸运的是,您有很多可用的解决方法:

  • print("The characters name is", King.name, "and age is", King.age)
  • print(f"The characters name is {King.name} and age is {King.age}") (需要 Python >= 3.6
  • print("The characters name is {King.name} and age is {King.age}".format(King=King))
  • print("The characters name is {} and age is {}".format(King.name, King.age))

  • ...

    关于python - 类型错误 : can only concatenate str (not "int") to str What does this mean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52045632/

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