0

What I want to do is determine the length of a number in python, for example I had a user input:

num = input("Enter a number: ")
print(num)

and the output was 1943, how will I get python to return the length of that, which is 4 in this example?

As well as that, how do I save it to a string? I tried this:

t = len(byte)

It just returns this:

Traceback (most recent call last):
File "bytes.py", line 10, in <module>
start()
File "bytes.py", line 5, in start
t = len(bytes)
TypeError: object of type 'type' has no len()
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
Okx
  • 353
  • 3
  • 23

2 Answers2

3

You can simply check its length as string:

print(len(str(num)))

Examples:

>>> num = 123123
>>> print(len(str(num)))
6

>>> num = 10293847586
>>> print(len(str(num)))
11
Community
  • 1
  • 1
sshashank124
  • 31,495
  • 9
  • 67
  • 76
1
print(len(num))

You already have a string. Just call len on it to get the length.

Your code failed because you typed len(bytes). Particularly, take note of that final s. bytes is the type Python uses to represent sequences of bytes; it's not the variable you used to store your input.

user2357112
  • 260,549
  • 28
  • 431
  • 505