报错代码:
a = input('请输入一个字符串:')
Sum = a.count('T')
Sum += a.count('t')
print('字母T共出现' + Sum + '次')
报错:
TypeError: can only concatenate str (not "int") to str
原因:
使用“+”时应为字符串和字符串的相加,但python不能自动转换,故可用str()方法强制转换
例如
print(str(1234)+'a')
报错代码:
a = input('请输入一个字符串:')
Sum = a.count('T')
Sum += a.count('t')
print('字母T共出现' + Sum + '次')
报错:
TypeError: can only concatenate str (not "int") to str
原因:
使用“+”时应为字符串和字符串的相加,但python不能自动转换,故可用str()方法强制转换
例如
print(str(1234)+'a')