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

Posted in /  

Python TypeError: object of type 'int' has no len() Solution
vinaykhatri

Vinay Khatri
Last updated on December 22, 2024

    In Python, we have a len() function that returns the total number of characters and items present in an iterable object such as string, tuple, dictionary and set. And when we try to perform the len() operation on an integer number, Python raises the error "TypeError: object of type 'int' has no len()".

    In this Python tutorial, we will discuss the reason behind this error and learn how to solve it. This Python tutorial also discusses a typical example scenario that demonstrates the "TypeError: object of type 'int' has no len()" error and its solution, so you could solve the one raising in your Python program. Let's get started with the Problem statement

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

    The len() is an inbuilt Python function that can accept an iterable object and return an integer number representing the total number of items present in that iterable object.

    Example

    >>> x = [20, 30, 40, 50]
    >>> len(x) #items present in the x
    4

    But if we try to pass an integer value as an argument to the len() function we receive the Error  " TypeError: object of type 'int' has no len() ".

    Example

    #integer number
    num = 300
    
    length = len(num)
    
    print(length)

    Output

    Traceback (most recent call last):
      File "main.py", line 4, in 
        length = len(num)
    TypeError: object of type 'int' has no len()

    In the above example, we are receiving the error because we are passing the num as an argument value to the len() function. And len() function return the error when an integer object or value is passed to it. The error statement has two sub statements separated with colon :

    1. TypeError
    2. object of type 'int' has no len()

    1. TypeError

    The TypeError is a standard Python exception. It is raised in a Python program when we try to perform an invalid operation on a Python object or when we pass an invalid data type to a function. In the above example, Python is raising the TypeError because the len() function expecting an iterable object data type and it received an int.

    2. object of type 'int' has no len()

    The statement " object of type 'int' has no len() " is the error message, that tag along with the TypeError exception. This message is putting the fact that the length function does not support the int value as an argument. This simply means we can not compute the length of an integer value using len() function.

    Common Example Scenario

    Many new Python learners are unaware of the fact that len() function can not operate on the int data type. And often they perform it on the integer object to get the total length or number of digits and encounter the error.

    Example

    Suppose we need to write a program that asks the user to enter a 4 digit number for the passcode. And we need to check if the entered number contains 4 digits or not.

    passcode = int(input("Enter a 4 Digit Number: "))
    
    #length of the passcode
    length = len(passcode)
    
    if length == 4:
        print("Your Entered Passcode is", passcode)
    else:
        print("Please only enter a 4 digit passcode")

    Output

    Enter a 4 Digit Number: 2342
    Traceback (most recent call last):
      File "main.py", line 4, in 
        length = len(passcode)
    TypeError: object of type 'int' has no len()

    Break the code

    In this example, we are getting the error with length = len(passcode) statement. This is because the passcode is an integer value, and len() function does not support int data values as an argument.

    Solution

    In the above example, our objective is to find the number of digits entered by the user. That can be found by the total length of the entered number. To solve the above problem we need to ensure that we are not passing the int value to the len() function. The input function accepts the entered passcode as a string, and we need to find the length for that entered string value. After getting the length we can convert that value to the int type.

    passcode = input("Enter a 4 Digit Number: ")
    
    #length of the passcode
    length = len(passcode)
    
    #covert the passcode into int
    passcode = int(passcode)
    
    if length == 4:
        print("Your Entered Passcode is", passcode)
    else:
        print("Please only enter a 4 digit passcode")

    Output

    Enter a 4 Digit Number: 4524
    Your Entered Passcode is 4524
    Now our code run successfully

    Conclusion

    The " TypeError: object of type 'int' has no len()" is a common error that many Python learners encounter when they accidentally pass an integer number to the len() function. To solve this problem we need to take care of the len() function and make sure that we are only passing an iterable object to it not a specific integer number.

    In this article, we have discussed this error in detail and also solve a common example.

    If you still getting this error in your Python program, please share your code and query in the comment section. We will try to help you in debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments