python中len函數
Python len()函數 (Python len() function)
len() functionis a library function in Python, it is used to get the length of an object (object may a string, list, tuple, etc). It accepts an object and returns its length (total number of characters in case of a string, the total number of elements in case of an iterable).
len()函數是Python中的庫函數,用于擷取對象的長度(對象可以是字元串,清單,元組等)。 它接受一個對象并傳回其長度(如果是字元串,則為字元總數,如果是可疊代的則為元素總數)。
Syntax: 句法:len(object)
Parameter: object – an object like string, list etc whose length to be calculated.
參數:object –一個對象,例如字元串,清單等,其長度将被計算。
Return value:int – returns length of the object.
傳回值:int –傳回對象的長度。
Example: 例:Input:
a = "Hello world!"
print(len(a))
Output:
12
Python code to get the length of an object (string, list etc) Python代碼擷取對象的長度(字元串,清單等) # python code to demonstrate an example
# of len() function
a = "Hello world!" # string value
b = [10, 20, 30, 40, 50] # list
c = ["Hello", "World!", "Hi", "Friends"] # list of strings
d = ("Hello", "World!", "Hi", "Friends") # Tuple
print("length of a: ", len(a))
print("length of b: ", len(b))
print("length of d: ", len(c))
print("length of c: ", len(d))
Output 輸出量 length of a: 12
length of b: 5
length of d: 4
length of c: 4
翻譯自: https://www.includehelp.com/python/len-function-with-example.aspx
python中len函數