我想从字符串末尾删除数字,但我不知道。
Can the split()
方法工作?我怎样才能做到这一点?
初始字符串看起来像asdfg123
,而我只想asdfg
反而。
感谢您的帮助!
不, split 不起作用,因为 split 只能与要分割的固定字符串一起使用。
你可以使用str.rstrip() method https://docs.python.org/3/library/stdtypes.html#str.rstrip:
import string
cleaned = yourstring.rstrip(string.digits)
这使用了string.digits持续的 https://docs.python.org/3/library/string.html#string.digits作为需要删除的内容的方便定义。
或者您可以使用正则表达式将末尾的数字替换为空字符串:
import re
cleaned = re.sub(r'\d+$', '', yourstring)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)