Python在一段句子里,如何提取出数字

Python在一段句子里,如何提取出数字不用re,句子有顾虑
2025-04-14 09:52:40
推荐回答(2个)
回答1:

为什么不能用re?

回答2:

不用re的话,可以用in来判断,代码如下:

testString = "hjkhks131fdsaf23413fasd123fafd123a1f3das1fasf1as1fa3f"
numberList = []
for _ in testString:
    if _ in ["1","2","3","4","5","6","7","8","9","0"]:
        print("当前字符是数字:"+_)
        numberList.append(_)

print(numberList)

运行效果见截图: