Python元組len()方法

2019-10-16 23:05:53

Python元組len()函式用於返回元組中的元素數量。

語法

以下是len()函式的語法 -

len(tuple)

引數

  • tuple - 這是一個要被計算元素個數的元組。

返回值

  • 此函式返回元組中的元素數量。

例子

以下範例顯示了len()函式的用法 -

#!/usr/bin/python3

tuple1, tuple2 = (123, 'xyz', 'zara'), (456, 'abc')
print ("First tuple length : ", len(tuple1))
print ("Second tuple length : ", len(tuple2))

執行上面程式碼,得到以下結果 -

First tuple length :  3
Second tuple length :  2