如何在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 生日广州男生祝福语简短