TypeError: object of type 'int' has no len() in Python [Fix]

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
7 min

banner

# Table of Contents

  1. TypeError: object of type 'int' has no len() in Python
  2. TypeError: object of type 'float' has no len() in Python
  3. TypeError: object of type 'bool' has no len() in Python

# TypeError: object of type 'int' has no len() in Python

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.

typeerror object of type int has no len

Here is an example of how the error occurs.

main.py
my_int = 100 # โ›”๏ธ TypeError: object of type 'int' has no len() print(len(my_int))

object of type int has no len

We passed an integer to the len() function which caused the error.

# Getting the length of an integer

If you need to get the length of an integer, convert it to a string first.

main.py
my_int = 100 print(len(str(my_int))) # ๐Ÿ‘‰๏ธ 3

getting the length of an integer

The len() function returns the length (the number of items) of an object.

main.py
my_list = ['apple', 'banana', 'kiwi'] result = len(my_list) print(result) # ๐Ÿ‘‰๏ธ 3
The argument the function takes may be a sequence (a string, tuple, list, range or bytes) or a collection (a dictionary, set, or frozen set).

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.

# Use the range() class to iterate a specific number of times

If you are trying to iterate a specific number of times, use the range() class.

main.py
my_int = 5 for n in range(my_int): print(n) result = list(range(my_int)) # ๐Ÿ‘‡๏ธ [0, 1, 2, 3, 4] print(result)

use range class to iterate specific number of times

The range() class is commonly used for looping a specific number of times in for loops and takes the following parameters:

NameDescription
startAn integer representing the start of the range (defaults to 0)
stopGo up to, but not including the provided integer
stepRange will consist of every N numbers from start to stop (defaults to 1)

# Track down where the variable got assigned an integer

If the variable got assigned an integer by mistake, you have to track down where the assignment is and correct it.

main.py
my_str = 'bobbyhadz.com' my_str = 100 # ๐Ÿ‘‡๏ธ TypeError: object of type 'int' has no len() print(len(my_str))

track down where variable got assigned an integer

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.

main.py
my_int = 5 print(dir(my_int))

Or you can check using a try/except statement.

main.py
my_int = 5 try: print(my_int.__len__) except AttributeError: # ๐Ÿ‘‡๏ธ this runs print('object has no attribute __len__')

using try except statement to handle the error

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.

main.py
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.

# Table of Contents

  1. TypeError: object of type 'float' has no len() in Python
  2. TypeError: object of type 'bool' has no len() in Python

# TypeError: object of type 'float' has no len() in Python

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.

typeerror object of type float has no len

Here is an example of how the error occurs.

main.py
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.

# Getting the length of a floating-point number

If you need to get the length of a float, convert it to a string first.

main.py
my_float = 3.14 print(len(str(my_float))) # ๐Ÿ‘‰๏ธ 4

If you need to exclude the period from the result, subtract 1.

main.py
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.

main.py
my_list = ['bobby', 'hadz', 'com'] result = len(my_list) print(result) # ๐Ÿ‘‰๏ธ 3
The argument the function takes may be a sequence (a string, tuple, list, range or bytes) or a collection (a dictionary, set, or frozen set).

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.

# Iterating a specific number of times

If you are trying to iterate a specific number of times, use the range() class.

main.py
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:

NameDescription
startAn integer representing the start of the range (defaults to 0)
stopGo up to, but not including the provided integer
stepRange 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.

# Track down where the variable got assigned a floating-point number

If the variable isn't supposed to store a float, you have to track down where it got assigned a float.

main.py
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.

main.py
my_float = 3.14 print(dir(my_float))

Or you can check using a try/except statement.

main.py
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.

main.py
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.

# TypeError: object of type 'bool' has no len() in Python

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.

typeerror object of type bool has no len

Here is an example of how the error occurs.

main.py
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.

# Evaluating an expression returns a boolean result

Make sure you aren't evaluating an expression in the call to the len() function.

main.py
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.

main.py
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.

main.py
my_list = ['bobby', 'hadz', 'com'] result = len(my_list) print(result) # ๐Ÿ‘‰๏ธ 3
The argument the function takes may be a sequence (a string, tuple, list, range or bytes) or a collection (a dictionary, set, or frozen set).

Notice that the len() function cannot be called with a boolean.

# Track down where the variable got assigned a boolean

We initially set the variable to a list, but later reassigned it to a boolean.

main.py
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.

main.py
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.

main.py
my_bool = True print(dir(my_bool))

Or you can check using a try/except statement.

main.py
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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev