我有一个像这样的字符串
string = "3,197Working age population"
我希望打破字符串,以便当数字3,197结束并且工作年龄人口开始使用正则表达式或任何其他有效方法时。总之,我只需要3,197
答案 0 :(得分:0)
您可以查看itertools.takewhile
:
from itertools import takewhile
string = "3,197Working age population"
r = ''.join(takewhile(lambda x: not x.isalpha(), string))
print(r)
# '3,197'
从字符串中取项,而尚未到达字母。使用join