len() function (To get length of various types of data)

len() function is very straightforward and easy to use. Let’s see some examples on different data types.

Estimated Time

5 mins

Skill Level

Beginner

Functions

len()

Course Provider

Provided by HolyPython.com

There can be many more reasons why you might want to use the len() function.

Below you can find a number of examples demonstrating how len() function in Python can be useful. These examples are great for understanding programming in general and in this case specifically len() function.

We also have great len() function exercises (currently approximately 5 of them) which is a wonderful way to support your learning process and conquer the topic.

Especially later on, when you start writing codes (whether in small or big projects) you’ll likely have many moments where you understand various orthodox or unorthodox use cases for len function in programming.

Function1 : len()

len() is a useful function that can show us the length of different data types.

Used Where?

len() function is a built-in Python function that works with different types of data.

  • it can tell length of a string (number of characters)
  • or it can tell the size of lists and tuples (number of elements)
  • it can also tell the size of a dictionary (number of unique keys)

The reason why you need this information in programming can vary greatly.

  • You might just be curious
  • You might be adding elements to your list or dictionary and want to know the new size (strings and tuples are mutable)
  • You might want to filter certain size of data types.
  • You might use a conditional statement for strings of certain sizes

Syntax do(s)

1) len() is used with an argument inside its parenthesis.

Syntax don't(s)

1) if you fail to specify an argument inside len() function's parenthesis you will get an error.

Example 1

>>> lst = [3, 33, 100, 1000, 5]
>>> print(len(lst))

5

Example 2

>>> my_msg = “Hello World!”
>>> print(len(my_msg))

12

Example 3

>>> my_tup = (“4k”, “1080p”, “720p”)
>>> print(len(my_tup))

3

Example 4

>>> my_dict = {“rainy”: 95, “sunny”: 270}
>>> print(len(my_dict))

2

Tips

1- As you can see, text in print function needs to be in quotes. Whenever you are printing a piece of text, this is the syntax you need to follow.

2- If you are printing a variable, then you can’t use quotes and you need to type the variable name only. Such as in Example 2

Example 5

>>> mymessage = “How about this?”
>>> print(mymessage)

How about this?

Advanced Concepts (Optional)

1-

Example 6

>>> print(“Hello” , “my name is” , “John”)

Hello my name is John

Next Lesson:

.sort() method

Caspar David Friedrich The Sea of Ice


Caspar David Friedrich

The Sea of Ice

The Sea of Ice by Caspar David Friedrich

“Have you installed Python yet?”

Having Python setup on your computer can expedite the learning experience and help you gain the real life skills in a non-simulation environment. Installing Python can have different nuances that can be confusing for everyone. We compiled a complete tutorial about the best and most convenient Python installing practices Check it out by clicking the button below.
Sandra Files