📅  最后修改于: 2023-12-03 15:02:40.172000             🧑  作者: Mango
The len()
function in Python 3 is used to determine the length of an object, such as a string, list, or tuple. Here is the syntax of the len()
function:
len(object)
The object
parameter can be any object in Python that has a length. The return value of the len()
function is an integer value that represents the number of elements in the object.
Let's use the len()
function to determine the length of a string:
string = "Python"
length = len(string)
print(length)
Output:
6
The len()
function has returned the length of the string which is 6.
Now, let's use the len()
function with a list:
my_list = [1, 2, 3, 4, 5]
length = len(my_list)
print(length)
Output:
5
The len()
function has returned the length of the list which is 5.
Finally, let's use the len()
function with a tuple:
my_tuple = (1, 2, 3, 4, 5)
length = len(my_tuple)
print(length)
Output:
5
The len()
function has returned the length of the tuple which is 5.
In conclusion, the len()
function in Python 3 is a useful function to determine the length of an object. It can be used with various objects such as strings, lists, or tuples.