|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #-----无视参数中的字符串-------#
- num = (1, 2, 7, 3, 10, 56, 'a', 'b')
- str1 = str(num)
- str2 = ''
- length = len(str1)
- i = 0
- j=0
- while i < length:
- if str1[i].isdigit():
- str2[j] = str1[i]
- i += 1
- j += 1
复制代码
运行的结果报错:- Traceback (most recent call last):
- File "C:\Users\asus\Desktop\python练习\016-2-1.py", line 10, in <module>
- str2[j] = str1[i]
- TypeError: 'str' object does not support item assignment
复制代码
求问各位大佬这是怎么回事?
str2 在你看来是什么变量?
在我看来这是字符变量 所以我用+来拼接,代码如下:
- #-----无视参数中的字符串-------#
- num = (1, 2, 7, 3, 10, 56, 'a', 'b')
- str1 = str(num)
- str2 = ''
- length = len(str1)
- i = 0
- j=0
- while i < length:
- if str1[i].isdigit():
- str2 += str1[i]
- i += 1
复制代码
|
|