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.

The length of Hello world

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

Count the letters: Hello contains 5 characters, and world contains 5 characters.

Count the space: The space between the two words is one additional character.

Combine the counts: Five letters plus five letters plus one space gives a total of 11 characters.

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.

len() countslen() countslen() countslen() countsStringcharactersIntegercharacter countListitemsIntegeritem countTupleitemsIntegeritem countDictionaryitemsIntegeritem count
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.

Finding the final valid index

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

Start with the length: The collection contains 11 characters.

Account for zero-based indexing: Because indexing starts at 0, the first character is at index 0 rather than index 1.

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

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.

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.

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

Compare the count with the requirement: The returned count, 4, is greater than the required count, 3.

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

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

  • Counting only letters in a string

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

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

  • Using the length as the final index

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

    Fix: Use length minus 1 to identify the final valid index.

  • Assuming every collection has the same kind of item

    The definition of item varies by data type.

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

Key Takeaways

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

Key Takeaways

  • 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.