python报错TypeError: must be str, not int

python报错TypeError: must be str, not int

字符串拼接一个整型变量报错,代码如下

for i in range(1,586):
    res = 'test' + i

报错信息如下

TypeError: must be str, not int

1个回答

第一种方法:

for i in range(1,586):
    res = 'test' + str(i)

第二种方法:

for i in range(1,586):
    res = 'test%s' % i #python3及之后的版本废弃

第三种方法:

for i in range(1,586):
    res = 'test{0}'.format(i) #Python 2.6引入

你可能感兴趣的:(python报错TypeError: must be str, not int)