如何在 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP