Trying to get the length of an integer value in Python and seeing the error "TypeError: object of type ‘int‘ has no len()"? This common error occurs because integers are simple numeric values that don‘t have a measurable length.
In this comprehensive guide, you‘ll learn exactly why this error happens and several simple ways to fix it.
What Does "Object of Type ‘int‘ has no len()" Mean?
This Python error message means you have attempted to use the len() function on a variable that is an integer (int) data type.
The len() function in Python returns the length or number of items in an object. It works on things like strings, lists, dictionaries and other objects that have multiple values.
But an integer is a simple numeric type representing a whole number. It‘s a single value that can‘t be broken down into smaller parts.
So when you try:
num = 100
print(len(num))
You get the "object of type ‘int‘ has no len()" error because integers don‘t have a "length".
This error typically occurs when you try to reuse code that works on strings, lists etc. but fails on integers.
Why You Can‘t Get the Length of an Integer
It helps to understand what integers represent in Python.
An int variable like 100 or 256211 is just a numeric value. It‘s not made up of any smaller parts or characters.
Strings like "Hello" are different – they are a sequence of characters. len("Hello") is 5 because the string has 5 individual letters.
Lists and tuples are also sequences containing multiple values. So len([1, 2, 3]) is 3 – the number of items in the list.
But an integer is a simple single numeric value with no constituent parts. It makes no sense to ask "What is the length of the integer 512?"
So integers have no length property and trying to get len() raises an error.
4 Simple Ways to Fix "Object of Type ‘int‘ has no len()"
While you can‘t get the length of an integer directly, you can convert it to another data type like a string, list, tuple or dict.
Then len() will work since these types have a length based on the number of elements they contain.
Here are 4 ways to fix the "object of type ‘int‘ has no len()" error by converting the integer:
1. Convert int to String
num = 100
# Convert to string
num = str(num)
print(type(num)) # <class ‘str‘>
print(len(num)) # 3
Converting the integer to a string lets us get the length, which will be the number of digits.
2. Convert int to List
num = 100
# Convert to list
num = [int(d) for d in str(num)]
print(type(num)) # <class ‘list‘>
print(len(num)) # 3
By converting the int to a string first, we can get each digit as a list element.
3. Convert int to Tuple
num = 100
# Convert to tuple
num = tuple(int(d) for d in str(num))
print(type(num)) # <class ‘tuple‘>
print(len(num)) # 3
Same concept as above but converting to a tuple instead of list.
4. Convert int to Dictionary
num = 100
# Convert to dictionary
num = {‘value‘: num}
print(type(num)) # <class ‘dict‘>
print(len(num)) # 1
A dictionary containing the integer has a length of 1.
Summary of Fixes
| Fix | How it Works |
|---|---|
| Convert to string | Gets length of digit characters in the integer |
| Convert to list | Splits integer into digits stored as separate list elements |
| Convert to tuple | Splits integer into digits stored as separate tuple elements |
| Convert to dict | Puts integer as a value in a dict – dict length is number of key-value pairs (just 1 for a single int) |
The key is converting the integer to a sequence or collection data type. Then len() can count the number of elements just like it would for a string, list, tuple etc.
Why These Solutions Work
These fixes work because they convert the integer into a type that has a measurable length.
The int is turned into a string, list, tuple or dict – all of which are sequence/collection types consisting of smaller parts:
- A string is a sequence of characters
- A list is a collection of elements
- A tuple is an immutable sequence of elements
- A dictionary contains key-value pairs
These types all have a length based on the number of characters, elements or pairs they contain.
So by converting the int to one of these types first, we make it possible to get the length when len() is called.
Conclusion
The "object of type ‘int‘ has no len()" error in Python occurs when you try to get the length of an integer variable.
Integers represent simple numeric values that don‘t consist of separate parts or elements. So the len() function cannot count any constituent components.
To fix this, you need to convert the int to a string, list, tuple or dict first.
These data types have a measurable length based on the number of characters, elements or key-value pairs they contain.
So by converting the int first, you can apply len() successfully and avoid the "object of type ‘int‘ has no len()" error.
I hope this guide has helped explain what causes this common Python error and given you simple ways to fix it. Let me know if you have any other questions!