Len [] là một chức năng tích hợp trong Python. Bạn có thể sử dụng Len [] để có độ dài của chuỗi, mảng, danh sách, tuple, từ điển, v.v. Bạn có thể sử dụng chức năng LEN để tối ưu hóa hiệu suất của chương trình. Số lượng các phần tử được lưu trữ trong đối tượng không bao giờ được tính toán, vì vậy LEN giúp cung cấp số lượng các phần tử.
Syntax:
len[value]
Parameters:
Giá trị: Giá trị đã cho bạn muốn độ dài của.: the given value you want the length of.
Giá trị trả về
Nó sẽ trả về một giá trị số nguyên, tức là độ dài của chuỗi đã cho hoặc mảng hoặc danh sách hoặc bộ sưu tập.
Các loại giá trị trả về khác nhau:
Strings:
Nó trả về số lượng ký tự trong một chuỗi, bao gồm dấu câu, không gian và tất cả các loại ký tự đặc biệt. Tuy nhiên, bạn nên rất cẩn thận trong khi sử dụng Len của một biến null.
Empty:
Trống là một cuộc gọi trả lại thứ hai có ký tự bằng không, nhưng nó luôn luôn không có.
Collections:
Len tích hợp trả về số lượng các yếu tố trong một bộ sưu tập.
TypeError:
Hàm Len phụ thuộc vào loại biến được truyền cho nó. Một loại không phải là không có bất kỳ hỗ trợ tích hợp nào.
Dictionary:
Đối với từ điển, mỗi cặp được tính là một đơn vị. Tuy nhiên, các giá trị và khóa không độc lập.
Ví dụ 1: Làm thế nào tìm thấy độ dài của chuỗi đã cho?
# testing len[] str1 = "Welcome to Guru99 Python Tutorials" print["The length of the string is :", len[str1]]
Output:
The length of the string is : 35
Ví dụ 2: Làm thế nào để tìm độ dài của danh sách trong Python?
# to find the length of the list list1 = ["Tim","Charlie","Tiffany","Robert"] print["The length of the list is", len[list1]]
Output:
The length of the list is 4
Ví dụ 3: Cách tìm độ dài của một tuple trong Python
# to find the length of the tuple Tup = ['Jan','feb','march'] print["The length of the tuple is", len[Tup]]
Output:
The length of the tuple is 3
Ví dụ 4: Làm thế nào để tìm độ dài của từ điển trong 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]]
Output:
The length of the Dictionary is 4
Ví dụ 5: Cách tìm độ dài của mảng trong Python
# to find the length of the array arr1 = ['Tim','Charlie','Tiffany','Robert'] print["The length of the Array is", len[arr1]]
Output:
# testing len[] str1 = "Welcome to Guru99 Python Tutorials" print["The length of the string is :", len[str1]]0
Summary:
- Len [] là một hàm tích hợp trong python. Bạn có thể sử dụng Len [] để có độ dài của chuỗi, mảng, danh sách, tuple, từ điển, v.v.
- Giá trị: Giá trị đã cho bạn muốn độ dài của.
- Giá trị trả về Trả về giá trị số nguyên, tức là độ dài của chuỗi đã cho, hoặc mảng, hoặc danh sách hoặc bộ sưu tập.
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc returns the length of the string.
Bàn luận
Hàm python len [] Trả về độ dài của chuỗi. & Nbsp;len[string]
Cú pháp python len []It returns an integer which is the length of the string.
Cú pháp: Len [chuỗi] & nbsp;
Return: nó trả về một số nguyên là độ dài của chuỗi. & Nbsp;
Python3
Ví dụ về Python Len []
# testing len[] str1 = "Welcome to Guru99 Python Tutorials" print["The length of the string is :", len[str1]]9
The length of the string is : 350
The length of the string is : 351
The length of the string is : 352
Output:
# testing len[] str1 = "Welcome to Guru99 Python Tutorials" print["The length of the string is :", len[str1]]1
Phương pháp Len [] với chuỗi trong Python.
# testing len[] str1 = "Welcome to Guru99 Python Tutorials" print["The length of the string is :", len[str1]]6
# testing len[] str1 = "Welcome to Guru99 Python Tutorials" print["The length of the string is :", len[str1]]7
# testing len[] str1 = "Welcome to Guru99 Python Tutorials" print["The length of the string is :", len[str1]]8
Ví dụ 1: hàm len [] với bộ dữ liệu và chuỗi
Ở đây chúng tôi đang đếm độ dài của các bộ dữ liệu và danh sách.
# testing len[] str1 = "Welcome to Guru99 Python Tutorials" print["The length of the string is :", len[str1]]9
The length of the string is : 350
The length of the string is : 351
# to find the length of the list list1 = ["Tim","Charlie","Tiffany","Robert"] print["The length of the list is", len[list1]]5
Python
# testing len[] str1 = "Welcome to Guru99 Python Tutorials" print["The length of the string is :", len[str1]]9
The length of the string is : 350
The length of the string is : 351
# to find the length of the tuple Tup = ['Jan','feb','march'] print["The length of the tuple is", len[Tup]]0
Output:
# testing len[] str1 = "Welcome to Guru99 Python Tutorials" print["The length of the string is :", len[str1]]2
Các
Python3
Output:
# testing len[] str1 = "Welcome to Guru99 Python Tutorials" print["The length of the string is :", len[str1]]3
# to find the length of the list
list1 = ["Tim","Charlie","Tiffany","Robert"]
print["The length of the list is", len[list1]]
6# testing len[]
str1 = "Welcome to Guru99 Python Tutorials"
print["The length of the string is :", len[str1]]
7 # to find the length of the list
list1 = ["Tim","Charlie","Tiffany","Robert"]
print["The length of the list is", len[list1]]
8The length of the string is : 35
6The length of the string is : 35
7The length of the string is : 35
8The length of the string is : 35
7# to find the length of the list
list1 = ["Tim","Charlie","Tiffany","Robert"]
print["The length of the list is", len[list1]]
0The length of the string is : 35
7The length of the list is 4
5The length of the list is 4
6
Python3
Ví dụ 2: Kiểu Python Len []
# testing len[] str1 = "Welcome to Guru99 Python Tutorials" print["The length of the string is :", len[str1]]9
The length of the string is : 350
The length of the string is : 351
The length of the tuple is 35
Ví dụ 3: Python Len [] với từ điển và bộ
# testing len[] str1 = "Welcome to Guru99 Python Tutorials" print["The length of the string is :", len[str1]]9
The length of the string is : 350
The length of the string is : 351
The length of the Dictionary is 40
Output:
# testing len[] str1 = "Welcome to Guru99 Python Tutorials" print["The length of the string is :", len[str1]]4
# to find the length of the tuple
Tup = ['Jan','feb','march']
print["The length of the tuple is", len[Tup]]
1# testing len[]
str1 = "Welcome to Guru99 Python Tutorials"
print["The length of the string is :", len[str1]]
7 # to find the length of the tuple
Tup = ['Jan','feb','march']
print["The length of the tuple is", len[Tup]]
3# to find the length of the tuple
Tup = ['Jan','feb','march']
print["The length of the tuple is", len[Tup]]
4# to find the length of the tuple
Tup = ['Jan','feb','march']
print["The length of the tuple is", len[Tup]]
5The length of the string is : 35
6# to find the length of the tuple
Tup = ['Jan','feb','march']
print["The length of the tuple is", len[Tup]]
7# to find the length of the tuple
Tup = ['Jan','feb','march']
print["The length of the tuple is", len[Tup]]
8# to find the length of the tuple
Tup = ['Jan','feb','march']
print["The length of the tuple is", len[Tup]]
9The length of the string is : 35
8The length of the tuple is 3
1
Python3
The length of the tuple is 36
# testing len[] str1 = "Welcome to Guru99 Python Tutorials" print["The length of the string is :", len[str1]]7
The length of the tuple is 38
The length of the string is : 356
# to find the length of the tuple Tup = ['Jan','feb','march'] print["The length of the tuple is", len[Tup]]7
The length of the string is : 358
# to find the length of the tuple Tup = ['Jan','feb','march'] print["The length of the tuple is", len[Tup]]7
# to find the length of the list list1 = ["Tim","Charlie","Tiffany","Robert"] print["The length of the list is", len[list1]]0__57454541
Ví dụ 4: Python Len [] với các đối tượng tùy chỉnh
The length of the Dictionary is 41
The length of the Dictionary is 42
The length of the Dictionary is 43
The length of the Dictionary is 44
The length of the Dictionary is 45
The length of the Dictionary is 46
The length of the Dictionary is 47
The length of the Dictionary is 48
The length of the Dictionary is 46
# to find the length of the array arr1 = ['Tim','Charlie','Tiffany','Robert'] print["The length of the Array is", len[arr1]]0
# testing len[] str1 = "Welcome to Guru99 Python Tutorials" print["The length of the string is :", len[str1]]7
# to find the length of the array arr1 = ['Tim','Charlie','Tiffany','Robert'] print["The length of the Array is", len[arr1]]2
The length of the Dictionary is 43
The length of the Dictionary is 44
# to find the length of the array arr1 = ['Tim','Charlie','Tiffany','Robert'] print["The length of the Array is", len[arr1]]5
The length of the Dictionary is 46
# to find the length of the array arr1 = ['Tim','Charlie','Tiffany','Robert'] print["The length of the Array is", len[arr1]]7
# testing len[] str1 = "Welcome to Guru99 Python Tutorials" print["The length of the string is :", len[str1]]9
The length of the string is : 350
The length of the string is : 351
# testing len[] str1 = "Welcome to Guru99 Python Tutorials" print["The length of the string is :", len[str1]]10
Output:
# testing len[] str1 = "Welcome to Guru99 Python Tutorials" print["The length of the string is :", len[str1]]5
The length of the Dictionary is 4
8# to find the length of the array
arr1 = ['Tim','Charlie','Tiffany','Robert']
print["The length of the Array is", len[arr1]]
9 The length of the Dictionary is 4
6# testing len[]
str1 = "Welcome to Guru99 Python Tutorials"
print["The length of the string is :", len[str1]]
01
# testing len[]
str1 = "Welcome to Guru99 Python Tutorials"
print["The length of the string is :", len[str1]]
02# testing len[]
str1 = "Welcome to Guru99 Python Tutorials"
print["The length of the string is :", len[str1]]
7 # testing len[]
str1 = "Welcome to Guru99 Python Tutorials"
print["The length of the string is :", len[str1]]
04# testing len[]
str1 = "Welcome to Guru99 Python Tutorials"
print["The length of the string is :", len[str1]]
05# to find the length of the list
list1 = ["Tim","Charlie","Tiffany","Robert"]
print["The length of the list is", len[list1]]
1function has time complexity of O[1]. in average and amortized case