Python Reference Library →

Python len() Function – Get the Length of Strings, Lists, and More

Let’s look at len() function – get the length of strings, lists, and more

  1. Notice

    len() returns the number of items in a sized object. For strings, it counts Unicode code points, not encoded bytes.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = 'tea'
print(len(text))
Live state

Top-level scope

text→Object 1
Object 1 · str'tea'
This step

len() returns the number of items in a sized object. For strings, it counts Unicode code points, not encoded bytes.

Picture len() function – get the length of strings, lists, and more

len() returns the number of items in a sized object. For strings, it counts Unicode code points, not encoded bytes.

Image brief

Alt text
A diagram for Python len() Function – Get the Length of Strings, Lists, and More. Instructions: text = 'tea' print(len(text)). Displayed result: 3. The final example bindings are text --> object 1 (str). object 1: 'tea'
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python len() Function – Get the Length of Strings, Lists, and More". Teaching focus: len() returns the number of items in a sized object. For strings, it counts Unicode code points, not encoded bytes. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = 'tea' print(len(text)) | v DISPLAYED RESULT 3 FINAL BINDINGS text --> object 1 (str) object 1: 'tea' Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python len() Function – Get the Length of Strings, Lists, and More. Instructions: text = 'tea' print(len(text)). Displayed result: 3. The final example bindings are text --> object 1 (str). object 1: 'tea'

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = 'tea'
print(len(text))
Live state

Top-level scope

text→Object 1
Object 1 · str'tea'
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
3

What do you expect this program to print?

What does this example display?

text = 'tea'
print(len(text))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

3

Follow the printed values in order. The output is: 3

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

len() returns the number of items in a sized object. For strings, it counts Unicode code points, not encoded bytes.

len() returns the number of items in a sized object. For strings, it counts Unicode code points, not encoded bytes.

More examples and reference

Python len() Function

The len() function in Python is used to get the number of items in an object. It works with many data types like strings, lists, tuples, dictionaries, and more.

Syntax

len(object)

Parameter:

  • object – A sequence (like string, list, tuple) or a collection (like dictionary, set, etc.).

Returns:

  • An integer representing the number of items in the object.

Example 1: Length of a String

text = "Hello, World!"
print(len(text))
13

Example 2: Length of a List

numbers = [1, 2, 3, 4, 5]
print(len(numbers))
5

Example 3: Length of a Tuple

values = (10, 20, 30)
print(len(values))
3

Example 4: Length of a Dictionary

person = {"name": "Alice", "age": 25, "city": "New York"}
print(len(person))
3

Note: len() returns the number of keys in the dictionary.

Supported Data Types

  • String
  • List
  • Tuple
  • Dictionary
  • Set
  • Range
  • Custom classes with __len__() method

Use Case: Why is len() Useful?

  • Checking if a list is empty: if len(my_list) == 0:
  • Limiting input size in forms or APIs
  • Looping through data and keeping count

Common Mistake

  • Wrong: len(5) → Raises TypeError because int has no length

Interview Tip

len() is often used in interview coding problems to control loops, check constraints, or validate data. Know which data types it works on!

Summary

  • len() returns the number of items in an object
  • Works with sequences and collections
  • Raises TypeError if used on unsupported types like int or float

Practice Problem

Ask the user to enter a sentence. Print how many characters it contains:

sentence = input("Enter a sentence: ")
print("Number of characters:", len(sentence))

What to remember

Connect what you noticed in the example with the idea below.

Remember

len() returns the number of items in a sized object. For strings, it counts Unicode code points, not encoded bytes.

Watch out for this idea

len() of text always equals the number of UTF-8 bytes.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →