Python元組len()方法

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

上一篇: Python元組 下一篇: Python字典