Introduction to Built-in Functions
len() is a built-in function that returns the number of items in its argument as an integer.
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
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').
- 1Count the letters
Hello contains 5 characters, and world contains 5 characters.
- 2Count the space
The space between the two words is one additional character.
- 3Combine 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.
| Collection type | What len() counts |
|---|---|
| String | Every character, including spaces and punctuation |
| List | Items in the list |
| Tuple | Items in the tuple |
| Dictionary | Items in the dictionary |
| Set | Items 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.
- 1Start with the length
The collection contains 11 characters.
- 2Account for zero-based indexing
Because indexing starts at 0, the first character is at index 0 rather than index 1.
- 3Subtract 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.
- 1Determine the collection length
Applying
len()gives an integer representing the number of items. In this example, the result is 4. - 2Compare the count with the requirement
The returned count, 4, is greater than the required count, 3.
- 3Make 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.
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 likeTreating the space in Hello world as if it were not present.
Why it failslen()counts every character, including spaces and punctuation.Do this insteadCount letters, spaces, punctuation, and other characters as individual items.
Common mistake Using the length as the final index
What it looks likeAssuming that a collection of length 11 has 11 as its highest valid index.
Why it failsIndices start at 0, so the highest valid index is one less than the length.
Do this insteadUse length minus 1 to identify the final valid index.
Common mistake Assuming every collection has the same kind of item
What it looks likeExpecting len() to count characters for a list or tuple.
Why it failsThe definition of item varies by data type.
Do this insteadIdentify 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. - 2For 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. - 4For indexed collections, the highest valid index is always the length minus 1.
- 5The 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.