如何在Python中消除字符串中的数字?
您可以创建一个数组来跟踪字符串中所有非数字字符。然后最后使用“”.join方法加入该数组。
示例
my_str = 'qwerty123asdf32'
non_digits = []
for c in my_str:
if not c.isdigit():
non_digits.append(c)
result = ''.join(non_digits)
print(result)输出结果
这将给出输出
qwertyasdf
示例
您还可以在一行中使用python列表理解来实现这一点。
my_str = 'qwerty123asdf32' result = ''.join([c for c in my_str if not c.isdigit()]) print(result)
输出结果
这将给出输出
qwertyasdf
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短