Python String Index() メソッド
パイソン 文字列インデックス() このメソッドを使用すると、ユーザーは指定された文字列内で最初に出現した既存の部分文字列のインデックスを見つけることができます。 パイソン 。
Python String Index() メソッドの構文
構文: string_obj.index(部分文字列、開始、終了)
パラメーター:
- 部分文字列: 検索する文字列。
- 開始 (デフォルト : 0) : この関数は、検索を開始する位置を指定します。
- end (デフォルト: 文字列の長さ): この関数は、検索を終了する必要がある位置を指定します。
戻る: 見つかった部分文字列の最初の位置を返します。
例外: 引数の文字列が見つからない場合、またはインデックスが範囲外の場合は、ValueError が発生します。
Python String Index() メソッドの例
ここで、文字列ランダムの「and」の最初の文字は「a」で、「a」のインデックスは 1 であるため、出力も 1 になります。
Python3
string> => 'random'> print> (> 'index of 'and' in string:'> , string.index(> 'and'> ))> |
出力
Index of ' and ' in string: 1
単一文字のインデックスを検索するための Python String Index() メソッド
Python string Index() メソッドの基本的な使用法は、特定の文字または単語のインデックス位置を指定することです。したがって、特定の文字のインデックスを見つける必要があるときは常に、 インデックスメソッド それを得るために。
Python3
# initializing target string> ch> => 'geeksforgeeks'> > # initializing argument string> ch1> => 'geeks'> > # using index() to find position of 'geeks'> # starting from 2nd index> # prints 8> pos> => ch.index(ch1,> 2> )> > print> (> 'The first position of geeks after 2nd index : '> ,end> => '')> print> (pos)> |
出力
The first position of geeks after 2nd index : 8
注記: Index() メソッドは次のようになります。 探す() 。唯一の違いは、検索された文字列が見つからない場合、find() は -1 を返し、この場合、index() は例外をスローすることです。
開始引数と終了引数を含む Python String Index()
の index()> Pythonのメソッド 文字列内で最初に出現する部分文字列のインデックスを見つけるために使用されます。部分文字列が見つかった場合はそのインデックスを返し、 ValueError> 部分文字列が存在しない場合。
Python3
test_string> => '1234gfg4321'> # finding gfg in string segment 'gfg4'> print> (test_string.index(> 'gfg'> ,> 4> ,> 8> ))> > # finding '21' in string segment 'gfg4321'> print> (test_string.index(> '21'> ,> 8> ,> len> (test_string)))> > # finding '32' in string segment 'fg432' using negative index> print> (test_string.index(> '32'> ,> 5> ,> -> 1> ))> |
出力
4 9 8
リスト内包表記を使用した Python String Index() メソッド
の index()> Pythonのメソッド 文字列内で最初に出現する部分文字列のインデックスを見つけるために使用されます。この例では、リスト内包表記を使用して、文字列内の複数の部分文字列のインデックスを検索します。
Python3
text> => 'Hello Geeks and welcome to Geeksforgeeks'> substring_list> => [> 'Geeks'> ,> 'welcome'> ,> 'notfound'> ]> > indices> => [text.index(sub)> if> sub> in> text> else> -> 1> for> sub> in> substring_list]> print> (indices)> |
出力
[6, 16, -1]
タプルを使用した Python String Index() メソッド
の index()> Pythonのメソッド 文字列内で最初に出現する部分文字列のインデックスを見つけるために使用されます。 Index() メソッドをタプルで使用すると、指定された文字列内のタプルから最初に出現した部分文字列のインデックスを見つけることができます。
Python3
text> => 'Hello Geeks! and welcome to Geeksforgeeks'> substring_tuple> => (> 'Geeks'> ,> 'to'> ,> '!'> ,> 'notfound'> )> > for> sub> in> substring_tuple:> > try> :> > index> => text.index(sub)> > print> (f> 'Index of '{sub}': {index}'> )> > except> ValueError as e:> > print> (f> 'Substring '{sub}' not found!'> )> |
出力
Index of 'Geeks': 6 Index of 'to': 25 Index of '!': 11 Substring 'notfound' not found!
Python String Index() メソッド使用時の例外
場合によっては、index() の場合に何らかのエラーにつながる例外が発生することがあります。ここでは、Value Error とも呼ばれるエラーが発生します。
値エラー: このエラーは、引数文字列がターゲット文字列内に見つからない場合に発生します。
パイソン
# initializing target string> ch> => 'geeksforgeeks'> > # initializing argument string> ch1> => 'gfg'> > # using index() to find position of 'gfg'> # raises error> pos> => ch.index(ch1)> > print> (> 'The first position of gfg is : '> ,end> => '')> print> (pos)> |
出力
Traceback (most recent call last): File '/home/aa5904420c1c3aa072ede56ead7e26ab.py', line 12, in pos = ch.index(ch1) ValueError: substring not found
実用化
パイソン String Index() メソッド関数を使用して、 ターゲット単語の前後の接尾辞または接頭辞の長さ 。以下の例では、AC 電圧からの命令の合計ビット長を、指定された情報で表示します。 弦 。
パイソン
# initializing target strings> VOLTAGES> => [> '001101 AC'> ,> '0011100 DC'> ,> '0011100 AC'> ,> '001 DC'> ]> > # initializing argument string> TYPE> => 'AC'> > # initializing bit-length calculator> SUM_BITS> => 0> > for> i> in> VOLTAGES:> > > ch> => i> > > if> ch[> len> (ch)> -> 2> ] !> => 'D'> :> > # extracts the length of bits in string> > bit_len> => ch.index(> TYPE> )> -> 1> > > # adds to total> > SUM_BITS> => SUM_BITS> +> bit_len> > print> (> 'The total bit length of AC is : '> , SUM_BITS)> |
出力
The total bit length of AC is : 13