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