意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

Python怎么从字符串中删除数字

来源:恒创科技 编辑:恒创科技编辑部
2024-05-06 14:41:01

有几种方法可以从字符串中删除数字:

  1. 使用正则表达式:
import re

text = "hello123world"
result = re.sub(r'\d', '', text)
print(result)
  1. 使用列表推导式:
text = "hello123world"
result = ''.join([char for char in text if not char.isdigit()])
print(result)
  1. 使用字符替换:
text = "hello123world"
result = ''.join(char for char in text if not char.isdigit())
print(result)

以上方法都会将字符串中的所有数字删除,返回一个不包含数字的新字符串。


Python怎么从字符串中删除数字

上一篇: oracle prior的作用有哪些 下一篇: python如何删除列表中指定元素