如何使用 Python 从字符串中提取数字?

pythonserver side programmingprogramming更新于 2024/2/24 13:18:00

从字符串中提取每个数字 −

>>> str1='a34e 345 bcd 5he 78 xyz'
>>> for s in str1:
if s.isdigit():print (s)

3
4
3
4
5
5
7
8

从以空格字符分隔的字符串中仅提取整数−

>>> str1='h3110 23 cat 444.4 rabbit 11 2 dog'
>>> for s in str1.split():
if s.isdigit():
print ((s))

23
11
2

相关文章