Pythonでは、文字列内の数値を見つけるためのいくつかの方法があります。以下にその方法をいくつか紹介します。
1. isdigit()メソッドを使用する
Pythonのisdigit()メソッドは、文字列がすべて数字であるかどうかを判定します。
new_string = "Germany26China47Australia88"
emp_str = ""
for m in new_string:
if m.isdigit():
emp_str = emp_str + m
print("Find numbers from string:", emp_str)
2. split()とappend()を使用する
split()とappend()を使用して、文字列から数値を抽出することも可能です。
new_str = "Micheal 89 George 94"
emp_lis = []
for z in new_str.split():
if z.isdigit():
emp_lis.append(int(z))
print("Find number in string:", emp_lis)
3. 正規表現を使用する
Pythonのreモジュールを使用して、正規表現で文字列を検索することもできます。
import re
re.search("P.th.n", "I love Python")
これらの方法を使えば、Pythonで文字列内の数値を効率的に見つけることができます。