Python报错can only concatenate str (not "int") to str

// 代码
thisset = set(("Google", "Running", "Taobao"))
print("length = "+ len(thisset))
// 报错
Traceback (most recent call last):
  File "D:\python\hello.py", line 276, in <module>
    print("length = "+ len(thisset))
TypeError: can only concatenate str (not "int") to str

分析

python并不能像java一样,在做拼接的时候自动把类型转换为string类型,需要再int型前调用str()转换为str的方法。
修改如下:

thisset = set(("Google", "Runoob", "Taobao"))
print("length = "+ str(len(thisset)))

你可能感兴趣的:(python)