如何在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