In Python, the len()
function is a built-in function that returns the number of elements in an object. It can be used with strings, lists, tuples, and other objects that support the len()
function.
Here is an example of how to use the len()
function with a string:
my_string = "hello"
length = len(my_string)
print(length)
This will output the following:
5
Here is an example of how to use the len()
function with a list:
my_list = [1, 2, 3, 4]
length = len(my_list)
print(length)
This will output the following:
4
The len()
function is a quick and easy way to get the length of an object. It is important to note that it only works with objects that support the len()
function, such as strings, lists, and tuples. It does not work with other data types, such as integers or floating point numbers.