Find Length of String, List or Array in Python using len() Function

In this tutorial, we are going to discuss how to get the length of string, list, array, object items using the Python len() function.  We will be looking into examples to get the length of the list in Python; get the length of string and array using the len() function in Python.

What is the Python len() function?

The len() is provided as a built-in function in Python. It returns the size of the data value passed to it as a parameter. The data value could be a text string, list, array, tuple etc.

Moreover, we will be checking how the Python len() function behaves on different types of data that is passed into it.

How to use Python len() with different values types?

Let’s have a quick look at how we can use the Python len() function for different types of values:

String Value as Parameter in len() function

When a string value is passed to the len() function, it will return a total number of characters in a text string including spaces.

string_data = "I am freaky"

# Get length of string
String_length = len(string_data)

print("Python string size is ->", String_length)
# OUTPUT Python string size is -> 11


Get length of Array List in Python using len() function

When array data is passed as a parameter to the len() function, then size or length is returned as shown below:

array_data = [1, 33, 56, 134]

# Get length of array
Array_length = len(array_data)

print("Python array size is ->", Array_length)
#OUTPUT Python array size is -> 4


Using Tuples as Parameters in len() function Example

Using Tuples, we can store multiple types of values in a single variable. Here we are going to pass a tuple variable to return its length using the Python len() function.

tuple_data = ("apple", 56, "banana", [1,2], 505)

# Get length of tupple
Tuple_length = len(tuple_data)

print("Python tuple size is ->", Tuple_length)
# OUTPUT Python tuple size is -> 5



We quickly discussed how to use len() function in Python with different types of data values passed to it. The len() behaves based on the type of data passed to it as a parameter.

Interestingly, when we pass a string as a parameter, it returns the length of characters. The len() function returns the size of the list when a list, array or tuple is passed.


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top