Last updated: Apr 8, 2024
Reading timeยท7 min
The Python "TypeError: object of type 'int' has no len()" occurs when we pass
an integer to the len()
function.
To solve the error, convert the integer to a string or correct the assignment
and pass a sequence (list
, str
, etc) to the len()
function.
Here is an example of how the error occurs.
my_int = 100 # โ๏ธ TypeError: object of type 'int' has no len() print(len(my_int))
We passed an integer to the len()
function which caused the error.
If you need to get the length of an integer, convert it to a string first.
my_int = 100 print(len(str(my_int))) # ๐๏ธ 3
The len() function returns the length (the number of items) of an object.
my_list = ['apple', 'banana', 'kiwi'] result = len(my_list) print(result) # ๐๏ธ 3
Notice that the len()
function cannot be called with an integer.
If you didn't expect the variable to store an integer value, you have to correct the assignment.
range()
class to iterate a specific number of timesIf you are trying to iterate a specific number of times, use the range()
class.
my_int = 5 for n in range(my_int): print(n) result = list(range(my_int)) # ๐๏ธ [0, 1, 2, 3, 4] print(result)
The range() class is commonly used for looping a specific number of times in for loops and takes the following parameters:
Name | Description |
---|---|
start | An integer representing the start of the range (defaults to 0 ) |
stop | Go up to, but not including the provided integer |
step | Range will consist of every N numbers from start to stop (defaults to 1 ) |
If the variable got assigned an integer by mistake, you have to track down where the assignment is and correct it.
my_str = 'bobbyhadz.com' my_str = 100 # ๐๏ธ TypeError: object of type 'int' has no len() print(len(my_str))
Initially, we set the variable to a string but it got set to a number later on which caused the error.
The argument the len()
function takes may be a sequence (a string, tuple,
list, range or bytes) or a collection (a dictionary, set, or frozen set).
When we pass an object to the len() function, the object's __len__() method is called.
You can use the dir()
function to print an object's attributes and look for
the __len__
attribute.
my_int = 5 print(dir(my_int))
Or you can check using a try/except statement.
my_int = 5 try: print(my_int.__len__) except AttributeError: # ๐๏ธ this runs print('object has no attribute __len__')
We try to access the object's __len__
attribute in the try
block and if an
AttributeError
is raised, we know the object doesn't have a __len__
attribute and cannot be passed to the len()
function.
If you aren't sure what type a variable stores, use the built-in type()
class.
my_int = 5 print(type(my_int)) # ๐๏ธ <class 'int'> print(isinstance(my_int, int)) # ๐๏ธ True
The type class returns the type of an object.
The isinstance() function
returns True
if the passed-in object is an instance or a subclass of the
passed in class.
The Python "TypeError: object of type 'float' has no len()" occurs when we
pass a float to the len()
function.
To solve the error, figure out where the variable got assigned a float and correct the assignment or convert the float to a string.
Here is an example of how the error occurs.
my_float = 3.14 # โ๏ธ TypeError: object of type 'float' has no len() print(len(my_float))
We passed a floating-point number to the len()
function which caused the
error.
If you need to get the length of a float, convert it to a string first.
my_float = 3.14 print(len(str(my_float))) # ๐๏ธ 4
If you need to exclude the period from the result, subtract 1.
my_float = 3.14 result = len(str(my_float)) - 1 print(result) # ๐๏ธ 3
The len() function returns the length (the number of items) of an object.
my_list = ['bobby', 'hadz', 'com'] result = len(my_list) print(result) # ๐๏ธ 3
Notice that the len()
function cannot be called with a floating-point number.
If you didn't expect the variable to store a float, you have to correct the assignment.
If you are trying to iterate a specific number of times, use the range()
class.
my_float = 3.14 for n in range(int(my_float)): print(n) result = list(range(int(my_float))) # ๐๏ธ [0, 1, 2] print(result)
The range() class is commonly used for looping a specific number of times in for loops and takes the following parameters:
Name | Description |
---|---|
start | An integer representing the start of the range (defaults to 0 ) |
stop | Go up to, but not including the provided integer |
step | Range will consist of every N numbers from start to stop (defaults to 1 ) |
Note that when we pass an object to the len() function, the object's __len__() method is called.
If the variable isn't supposed to store a float, you have to track down where it got assigned a float.
my_str = 'bobbyhadz.com' my_str = 3.14 # โ๏ธ TypeError: object of type 'float' has no len() print(len(my_str))
The variable is initialized as a string, but it gets set to a floating-point number later on.
Passing the floating-point number to the len()
function causes the error.
You have to track down where the variable got assigned a float in your code and correct the assignment.
You can use the dir()
function to print an object's attributes and look for
the __len__
attribute.
my_float = 3.14 print(dir(my_float))
Or you can check using a try/except
statement.
my_float = 3.14 try: print(my_float.__len__) except AttributeError: # ๐๏ธ this runs print('object has no attribute __len__')
We try to access the object's __len__
attribute in the try
block and if an
AttributeError
is raised, we know the object doesn't have a __len__
attribute and cannot be passed to the len()
function.
If you aren't sure what type a variable stores, use the built-in type()
class.
my_float = 3.14 print(type(my_float)) # ๐๏ธ <class 'float'> print(isinstance(my_float, float)) # ๐๏ธ True
The type class returns the type of an object.
The isinstance() function
returns True
if the passed-in object is an instance or a subclass of the
passed-in class.
The Python "TypeError: object of type 'bool' has no len()" occurs when we pass
a boolean value (True
or False
) to the len()
function.
To solve the error, make sure you aren't evaluating an expression in the call
to the len()
function.
Here is an example of how the error occurs.
my_bool = True # โ๏ธ TypeError: object of type 'bool' has no len() print(len(my_bool))
We passed a boolean (True
or False
) to the len()
function which caused the
error.
Make sure you aren't evaluating an expression in the call to the len()
function.
my_bool = True # โ๏ธ TypeError: object of type 'bool' has no len() if len('hi' == 'hi'): print('success')
The expression 'hi' == 'hi'
evaluates to True
, so we end up passing a
boolean to the len
function.
Instead, you can call the len()
function with each value.
my_bool = True if len('hi') == len('hi'): # ๐๏ธ this runs print('success')
The len() function returns the length (the number of items) of an object.
my_list = ['bobby', 'hadz', 'com'] result = len(my_list) print(result) # ๐๏ธ 3
Notice that the len()
function cannot be called with a boolean.
We initially set the variable to a list, but later reassigned it to a boolean.
my_list = ['bobby', 'hadz', 'com'] my_list = True # โ๏ธ TypeError: object of type 'bool' has no len() print(len(my_list))
Passing a boolean value to the len()
function causes the error.
You have to track down where the variable got set to a boolean and correct the assignment.
If you didn't expect the variable to store a boolean value, you have to correct the assignment.
The argument the function len()
takes may be a sequence (a string, tuple,
list, range or bytes) or a collection (a dictionary, set, or frozen set).
If you aren't sure what type a variable stores, use the built-in type()
class.
my_bool = False print(type(my_bool)) # ๐๏ธ <class 'bool'> print(isinstance(my_bool, bool)) # ๐๏ธ True
The type class returns the type of an object.
The isinstance() function
returns True
if the passed-in object is an instance or a subclass of the
passed-in class.
When we pass an object to the len() function, the object's __len__() method is called.
You can use the dir()
function to print an object's attributes and look for
the __len__
attribute.
my_bool = True print(dir(my_bool))
Or you can check using a try/except
statement.
my_bool = True try: print(my_bool.__len__) except AttributeError: # ๐๏ธ this runs print('object has no attribute __len__')
We try to access the object's __len__
attribute in the try
block and if an
AttributeError
is raised, we know the object doesn't have a __len__
attribute and cannot be passed to the len()
function.
You can learn more about the related topics by checking out the following tutorials: