如何使用 Python 從字串中提取數字?


從字串中提取每個數字 −

>>> 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

更新於:2020 年 6 月 23 日

581 次瀏覽

開啟您的 職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.