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

Python 文字列の長さ | len() メソッドの例


len() は Python の組み込み関数です。 len() を使用して、指定された文字列、配列、リスト、タプル、辞書などの長さを取得できます。

len 関数を使用して、プログラムのパフォーマンスを最適化できます。オブジェクトに格納されている要素の数は計算されないため、len は要素の数を提供するのに役立ちます。

構文:

len(value)

パラメータ:

価値 :長さが必要な特定の値。

戻り値

整数値、つまり、指定された文字列、配列、リスト、またはコレクションの長さを返します。

さまざまな種類の戻り値:

文字列:

句読点、スペース、およびすべてのタイプの特殊文字を含む、文字列内の文字数を返します。ただし、Null 変数の len を使用するときは十分に注意する必要があります。

空:

Empty は 0 文字の 2 回目のリターン コールですが、常に None です。

コレクション:

len ビルトインは、コレクション内の要素の数を返します。

TypeError:

Len 関数は、渡される変数の型によって異なります。 Non-Type にはサポートが組み込まれていません。

辞書:

ディクショナリでは、各ペアが 1 つの単位としてカウントされます。ただし、値とキーは独立していません。

例 1:指定された文字列の長さを調べる方法

# testing len() 
str1 = "Welcome to  Guru99 Python Tutorials"
print("The length of the string  is :", len(str1))

出力:

The length of the string  is : 35

例 2:Python でリストの長さを調べる方法

# to find the length of the list

list1 = ["Tim","Charlie","Tiffany","Robert"]

print("The length of the list is", len(list1))

出力:

The length of the list is 4

例 3:Python でタプルの長さを見つける方法

# to find the length of the tuple

Tup = ('Jan','feb','march')

print("The length of the tuple is", len(Tup))

出力:

The length of the tuple is 3

例 4:Python で辞書の長さを調べる方法

# to find the length of the Dictionary

Dict = {'Tim': 18,'Charlie':12,'Tiffany':22,'Robert':25}

print("The length of the Dictionary is", len(Dict))

出力:

The length of the Dictionary is 4

例 5:Python で配列の長さを見つける方法

# to find the length of the array

arr1 = ['Tim','Charlie','Tiffany','Robert']

print("The length of the Array is", len(arr1))

出力:

The length of the Array is 4

まとめ:


Python

  1. Java の String Length() メソッド:例で見つける方法
  2. Java String charAt() メソッドと例
  3. Java String contains() メソッド |部分文字列を例でチェック
  4. 例を使用したJava文字列のendsWith()メソッド
  5. 例を使用した Python 文字列 strip() 関数
  6. 例を使用した Python 文字列 count()
  7. Python String format() 例で説明
  8. Python 文字列の長さ | len() メソッドの例
  9. Python String find() メソッドと例
  10. Yield in Python チュートリアル:Generator &Yield vs Return の例
  11. 例を使用したコレクション内の Python カウンター