【问题标题】:First time using coding python, how I fix the can only concatenate str (not "int") to str第一次使用编码python,我如何修复只能将str(不是“int”)连接到str
【发布时间】:2021-11-29 23:29:26
【问题描述】:
phrase = "This is amazing"
print("How many characters in the variable phrase " + len(phrase) + "this are the numbers")

它说只能将str(不是“int”)连接到str

【问题讨论】:

标签: python string integer concatenation


【解决方案1】:

情况是您不能将字符串与整数连接,因为 len(phrase) 从短语返回长度,这就是您收到该错误的原因。所以你不能将整数转换为字符串,然后以这种方式连接:

phrase = "This is amazing"
print("How many characters in the variable phrase " + str(len(phrase)) + "this are the numbers").

我建议在连接字符串时使用 join 函数以获得更好的结果

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-03
    • 1970-01-01
    • 2020-10-06
    • 2021-02-15
    • 1970-01-01
    • 2020-10-20
    • 2018-12-17
    相关资源
    最近更新 更多