Concepts / Introduction to Built-in Functions

Introduction to Built-in Functions

len() is a built-in function that returns the number of items in its argument as an integer.

  • Programming

A Length Question

A built-in function is a function available for use in Python. The len() built-in function answers a simple but useful question: how many items are in the value given to it? It returns that count as an integer.

What do you think happens?

What integer should len('Hello world') return? Count every character before revealing your answer.

Reveal answer
Answer:

11

The string contains the five letters in Hello, one space, and the five letters in world. The space is also counted as a character.

Counting Characters

When len() receives a string, it counts every character in that string. Letters, digits, spaces, and punctuation each count as individual items. It does not count only the visible words or only the letters.

Worked example

The length of Hello world

Determine the result of len('Hello world').

  1. 1
    Count the letters

    Hello contains 5 characters, and world contains 5 characters.

  2. 2
    Count the space

    The space between the two words is one additional character.

  3. 3
    Combine the counts

    Five letters plus five letters plus one space gives a total of 11 characters.

Result

len('Hello world') returns 11.

Different Collections, Different Items

len() is not limited to strings. It can operate on collection types including lists, tuples, dictionaries, and sets. The meaning of an item varies with the data type, but the central principle stays the same: len() returns the number of items in the collection.

StringcharactersIntegercharacter countListitemsIntegeritem countTupleitemsIntegeritem countDictionaryitemsIntegeritem countlen() countslen() countslen() countslen() counts
What exactly is being counted when len() receives different collection types?
Collection typeWhat len() counts
StringEvery character, including spaces and punctuation
ListItems in the list
TupleItems in the tuple
DictionaryItems in the dictionary
SetItems in the set

The definition of item depends on the collection type.

Length and Index Positions

For an indexed collection, the length connects directly to its valid index positions. Indices start at 0, so the highest valid index is always one less than the collection's length. If a string has length 11, its valid indices run from 0 through 10.

Worked example

Finding the final valid index

A string has length 11. Determine its highest valid index.

  1. 1
    Start with the length

    The collection contains 11 characters.

  2. 2
    Account for zero-based indexing

    Because indexing starts at 0, the first character is at index 0 rather than index 1.

  3. 3
    Subtract one

    The highest valid index is the length minus 1: 11 minus 1 equals 10.

Result

The valid indices run from 0 through 10, and the final character is at index 10.

Practical Size Checks

The integer returned by len() can be used when a problem depends on collection size. For example, you can determine whether a collection contains enough items for a task by comparing its returned length with the required amount. The important sequence is: provide the collection to len(), receive an integer, then use that count to make the size decision.

Worked example

Checking a required collection size

A task requires a collection to contain at least 3 items. A collection has length 4. Decide whether it has enough items.

  1. 1
    Determine the collection length

    Applying len() gives an integer representing the number of items. In this example, the result is 4.

  2. 2
    Compare the count with the requirement

    The returned count, 4, is greater than the required count, 3.

  3. 3
    Make the decision

    Because the collection has at least 3 items, it has enough items for this requirement.

Result

The collection passes the size check because its length is 4.

EASY

A string has 8 characters. What is its highest valid index, and why?

Hints
  • Indices start at 0.
  • The highest valid index is one less than the length.

Mistakes with Collection Length

  • Common mistake

    Counting only letters in a string

    What it looks like

    Treating the space in Hello world as if it were not present.

    Why it fails

    len() counts every character, including spaces and punctuation.

    Do this instead

    Count letters, spaces, punctuation, and other characters as individual items.

  • Common mistake

    Using the length as the final index

    What it looks like

    Assuming that a collection of length 11 has 11 as its highest valid index.

    Why it fails

    Indices start at 0, so the highest valid index is one less than the length.

    Do this instead

    Use length minus 1 to identify the final valid index.

  • Common mistake

    Assuming every collection has the same kind of item

    What it looks like

    Expecting len() to count characters for a list or tuple.

    Why it fails

    The definition of item varies by data type.

    Do this instead

    Identify the collection type first, then interpret the returned integer as that type's item count.

Key Takeaways

  1. 1
    len() is a built-in function that returns the number of items in its argument as an integer.
  2. 2
    For strings, len() counts every character, including spaces and punctuation.
  3. 3
    len() also works with lists, tuples, dictionaries, and sets, although the meaning of item varies by data type.
  4. 4
    For indexed collections, the highest valid index is always the length minus 1.
  5. 5
    The integer returned by len() can be used to solve collection-size problems.

At a glance

  • len() returns a collection's number of items as an integer.
  • Strings are counted character by character, including spaces and punctuation.
  • The length of an indexed collection is one more than its highest valid index.
  • The meaning of an item depends on the collection type.
  • The returned length can support practical checks about whether a collection is large enough.

Related concepts

More concepts starting with I