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

例を使用した Python 文字列 count()

Python カウント

count() は Python の組み込み関数です。文字列内の特定の要素の合計数を返します。カウントは、文字列の最初から最後まで始まります。検索を開始する開始インデックスと終了インデックスを指定することもできます。

この Python チュートリアルでは、次のことを学びます:

PythonString Count() の構文

Python カウント関数の構文:

string.count(char or substring, start, end)

Python 構文のパラメーター

戻り値

count() メソッドは、整数値、つまり、指定された文字列から指定された要素の数を返します。指定された文字列に値が見つからない場合は、0 を返します。

例 1:文字列のカウント メソッド

次の例は、文字列に対する count() 関数の動作を示しています。

str1 = "Hello World"
str_count1 = str1.count('o')  # counting the character “o” in the givenstring
print("The count of 'o' is", str_count1)

str_count2 = str1.count('o', 0,5)
print("The count of 'o' usingstart/end is", str_count2)

出力:

The count of 'o' is 2
The count of 'o' usingstart/end is 1

例 2:特定の文字列内の文字の出現回数をカウントする

次の例は、開始/終了インデックスを使用して、特定の文字列と同様に文字の出現を示しています。

str1 = "Welcome to Guru99 Tutorials!"
str_count1 = str1.count('u')  # counting the character “u” in the given string
print("The count of 'u' is", str_count1)

str_count2 = str1.count('u', 6,15)
print("The count of 'u' usingstart/end is", str_count2)

出力:

The count of 'u' is 3
The count of 'u' usingstart/end is 2

例 3:特定の文字列内の部分文字列の出現回数をカウントする

次の例は、特定の文字列での部分文字列の出現と、start/endindex の使用を示しています。

str1 = "Welcome to Guru99 - Free Training Tutorials and Videos for IT Courses"
str_count1 = str1.count('to') # counting the substring “to” in the givenstring
print("The count of 'to' is", str_count1)
str_count2 = str1.count('to', 6,15)
print("The count of 'to' usingstart/end is", str_count2)

出力:

The count of 'to' is 2
The count of 'to' usingstart/end is 1

まとめ:


Python

  1. 部分文字列と例を含む Java String indexOf() メソッド
  2. Java String compareTo() メソッド:例での使用方法
  3. 例を使用した Python 文字列 strip() 関数
  4. Python String format() 例で説明
  5. Python String find() メソッドと例
  6. 例を含む Python Lambda 関数
  7. 例を使用した Python round() 関数
  8. 例を使用した Python map() 関数
  9. Python Timeit() と例
  10. 例を使用したコレクション内の Python カウンター
  11. Python の type() と isinstance() と例