工業製造
産業用モノのインターネット | 工業材料 | 機器のメンテナンスと修理 | 産業プログラミング |
home  MfgRobots >> 工業製造 >  >> Industrial programming >> Python

Python String.find()メソッド完全ガイド:構文、例、代替手法

Python String.find()とは

String.find()メソッドは、文字列内で指定された部分文字列が最初に出現するインデックスを返します。部分文字列が見つからない場合、例外を発生させずに-1を返すため、部分文字列検索に便利な選択肢となります。

このガイドでは、find()の効果的な使い方、パラメータの探求、関連メソッドとの比較、部分文字列の出現回数カウントまで学びます。

Python string find()の構文

メソッドのシグネチャはシンプルです:

string.find(substring, start, end)

パラメータ

デフォルト値でのfind()使用

startやendを指定しない場合、find()は文字列全体を先頭からスキャンします。最初の一致インデックスを返し、見つからなければ-1を返します。

例:

mystring = "Meet Guru99 Tutorials Site.Best site for Python Tutorials!"
print("The position of Tutorials is at:", mystring.find("Tutorials"))

出力:

The position of Tutorials is at: 12

開始インデックスの指定

start引数を指定すると、検索をその位置から開始し、文字列の末尾まで延長します。

例:

mystring = "Meet Guru99 Tutorials Site.Best site for Python Tutorials!"
print("The position of Tutorials is at:", mystring.find("Tutorials", 20))

出力:

The position of Tutorials is at 48

開始と終了範囲の定義

startとendの両方を指定すると、文字列の特定スライスに検索を限定できます。

例:

mystring = "Meet Guru99 Tutorials Site.Best site for Python Tutorials!"
print("The position of Tutorials is at:", mystring.find("Tutorials", 5, 30))

出力:

The position of Tutorials is at 12

位置検索と存在しない部分文字列の扱い

find()の動作を、部分文字列が存在する場合と存在しない場合で示します。

例:

mystring = "Meet Guru99 Tutorials Site.Best site for Python Tutorials!"
print("The position of Best site is at:", mystring.find("Best site", 5, 40))
print("The position of Guru99 is at:", mystring.find("Guru99", 20))

出力:

The position of Best site is at: 27
The position of Guru99 is at: -1

rfind()との比較

rfind()はfind()と似ていますが、部分文字列が出現する最大インデックスを返します。部分文字列が存在しない場合、両メソッドとも-1を返します。

例:

mystring = "Meet Guru99 Tutorials Site.Best site for Python Tutorials!"
print("The position of Tutorials using find() : ", mystring.find("Tutorials"))
print("The position of Tutorials using rfind() : ", mystring.rfind("Tutorials"))

出力:

The position of Tutorials using find() : 12
The position of Tutorials using rfind() : 48

index()との比較

index()メソッドはfind()と同様に振る舞いますが、部分文字列が見つからないときValueErrorを発生させます。

例:

mystring = "Meet Guru99 Tutorials Site.Best site for Python Tutorials!"
print("The position of Tutorials using find() : ", mystring.find("Tutorials"))
print("The position of Tutorials using index() : ", mystring.index("Tutorials"))

出力:

The position of Tutorials using find() : 12
The position of Tutorials using index() : 12

部分文字列が見つからない場合:

mystring = "Meet Guru99 Tutorials Site.Best site for Python Tutorials!"
print("The position of Tutorials using find() : ", mystring.find("test"))
print("The position of Tutorials using index() : ", mystring.index("test"))

出力:

The position of Tutorials using find() : -1
Traceback (most recent call last):
File "task1.py", line 3, in 
print("The position of Tutorials using index() : ", mystring.index("test"))
ValueError: substring not found

部分文字列出現回数のカウント

部分文字列の出現回数を数えるには、文字列をイテレートし、更新された開始インデックスで繰り返しfind()を呼び出します。

例:

my_string = "test string test, test string testing, test string test string"
startIndex = 0
count = 0
for _ in range(len(my_string)):
    k = my_string.find('test', startIndex)
    if k != -1:
        startIndex = k + 1
        count += 1
print("The total count of substring test is:", count)

出力:

The total count of substring test is: 6

重要なポイント

Python

  1. Python strip()メソッド完全ガイド:実践例付き
  2. Python str.count()マスター:文字と部分文字列のカウント実例集
  3. Python format()文字列を明確な例でマスター
  4. Python len()関数:オブジェクトの長さを測る実践ガイド
  5. Pythonラムダ関数をマスターする:実例とベストプラクティス
  6. Python round()関数を実例で徹底解説
  7. Python map()関数をマスター:構文、例、ベストプラクティス
  8. Python timeit():実行時間測定の実例付きガイド
  9. Python type()とisinstance()の理解:実例付き解説
  10. Java String.length()メソッド:文字列サイズ取得の実例解説
  11. Java String.indexOf()マスター:部分文字列検索と実践例