The TypeError: object of type ‘float’ has no len() happens because the len() function does not accept the float object as a parameter to the function. To fix the error, follow the article to understand better
TypeError: object of type ‘float’ has no len() in Python
Example:
floatVar = 1.2 # Command line caused the error print(len(floatVar))
Output:
Traceback (most recent call last):
File "./prog.py", line 4, in <module>
TypeError: object of type 'float' has no len()
Note: The len() function will accept the following objects as arguments to a valid function: string, bytes, tuple, list, range, dictionary, set, or frozen set.
How to solve this error?
Convert float values to other objects
As I should have shown the objects that the len() function can take as a parameter, you only need to change the float object to valid objects by the len() function.
Example:
- Convert the float object to valid types as the len() function parameter. I take a few styles as examples.
- Use the len() function to find the length of that object.
# The data type is a list myList = [1.2] #T he data type is a string myString = '1.2' # The data type is a tuple myTuple = (1.2,) # The data type is a set mySet = {1.2} # The data type is a dict myDict = {'Float' : 1.2} print('The length of the list is:', len(myList)) print('The length of the string is:', len(myString)) print('The length of the tuple is:', len(myTuple)) print('The length of the set is:', len(mySet)) print('The length of the dict is:', len(myDict))
Output:
The length of the list is: 1
The length of the string is: 3
The length of the tuple is: 1
The length of the set is: 1
The length of the dict is: 1
Use the isinstance() function
Syntax:
isinstance(Object, classinfo)
Parameters:
- Object: the object you want to check.
- classinfo: class, type, or tuple.
Example:
- Initialize a float.
- Use the isinstance() function to check if the number is a float object.
floatVar = 1.2 # Use the isinstance() function to check if the number is a float object if isinstance(floatVar, float): print ("Value is float") else: print ('Object length:', len(floatVar))
Output:
Value is float
Use the type() function to check the data
Example:
- Initialize a float.
- Initialize a float.
- Use the type() function to check if objects are data types that can be len() function parameters, then print the length of those objects. Do not execute the ‘else’ command.
floatVar = 1.2 if type(floatVar) in (str, bytes, tuple, list, range, dict, set, frozenset): print (len(floatVar)) else: print ('The len() function does not accept this data type.')
Output:
The len() function does not accept this data type.
Summary
If you have any questions about the the TypeError: object of type ‘float’ has no len() in Python, please leave a comment below. I will answer your questions. Thank you for reading!
Maybe you are interested:
- TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘str’
- TypeError: missing 1 required positional argument: ‘self’
- TypeError: Object of type bytes is not JSON serializable

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java