256 Kilobytes

Answers in Scripting and Automation | By Some Guy

Published

755 views, 0 RAMs, and 1 comment

Tags: Python, Scripting, Errors, Programming, TypeError

Profile Photo - Some GuySome GuyBasic Subscriber

Hello! I'm very new to Python, so sorry if this question is super simple. But I keep running into this message: TypeError: object of type 'int' has no len() What does this mean? Where am I going wrong in my code? How do I fix it? Thanks in advance.

Download more RAM. 🐏 ⨉ 0Posted by Some Guy 2 years ago 🕓 Posted at 13 January, 2019 05:53 AM PST

This is a debug account.

Profile Photo - Hash BrownHash BrownInternet ActivistEnglandStaff

Hello dude,

len() gets the length of a string. The data passed to it has to be a string, that's just how it works.

So if you're trying to pass an interger value into it then it will break and you will see your type error.

To get the len() (number of characters) of an int you need to convert it to a string before running the function.

x = 12345

len(str(x))

You can also do this without converting the value to a string with the math library.

import math

x = 12345
digits = int(math.log10(x))+1

And if you need to deal with zeros or negative numbers you can do something liike this.

import math

x = 12345
if x > 0:
    digits = int(math.log10(x))+1
elif x == 0:
    digits = 1
else:
    digits = int(math.log10(-x))+2

It's a bit of extra work but in some cases you may need to do that. It depends on what you're trying to do.

Hope this helps.

Download more RAM. 🐏 ⨉ 0Posted by Hash Brown 2 years ago 🕓 Posted at 20 February, 2019 03:40 AM PST

"THAT DOG IS GETTING RAPED" - Terry A. Davis

Post a New Comment

Do you like having a good time?

Register an Account

You can also login to an existing account or reset your password. All use of this site is subject to the terms of service and privacy policy.

Read Quality Articles

Read some quality articles. If you can manage to not get banned for like five minutes, you can even post your own articles.

View Articles →

Argue with People on the Internet

Use your account to explain why people are wrong on the Internet forum.

View Forum →

Vandalize the Wiki

Or don't. I'm not your dad.

View Wiki →

Ask and/or Answer Questions

If someone asks a terrible question, post a LMGTFY link.

View Answers →

Make Some Money

Hire freelancers and/or advertise your goods and/or services. Hire people directly. We're not a middleman or your dad. Manage your own business transactions.

Register an Account

Answers— Read More

Find more related content below!