You can get the length of an integer in Python in *any* of the following ways:

- Converting to String and Checking the Length;
- Converting to
`list`

and Checking the Length; - Looping and Removing Digits Off the End;
- Calculating the Number of Digits.

## Converting to String and Checking the Length

If you have a positive integer, you can do the following:

- Convert integer to string (using
`str()`

); - Call the
`len()`

method on the resulting string.

num = 12345 num_str = str(num) print(len(num_str)) # 5

If the number can potentially be negative, then you *must* first get the absolute value of the integer:

num = -12345 num_str = str(abs(num)) print(len(num_str)) # 5

To make it reusable, you could make this into a function, for example, like so:

def num_length(num): return len(str(abs(num))) print(num_length(0)) # 1 print(num_length(12345)) # 5 print(num_length(-12345)) # 5 print(num_length(9999999999999999999999999)) # 25 print(num_length(-9999999999999999999999999)) # 25

## Converting to `list`

and Checking the Length

If you have a positive integer, you can do the following:

- Convert integer to a list of digits;
- Call the
`len()`

method on the resulting list of digits.

For example, you can do this using list comprehension like so:

num = 12345 digits = [int(x) for x in str(num)] print(len(digits)) # 5

You can rewrite the same code using `map()`

like so:

num = 12345 digits = list(map(int, str(num))) print(len(digits)) # 5

If the number can potentially be negative, then you *must* first get the absolute value of the integer:

num = -12345 digits = [int(x) for x in str(abs(num))] print(len(digits)) # 5

To make it reusable, you could make this into a function, for example, like so:

def num_length(num): digits = [int(x) for x in str(abs(num))] return len(digits) print(num_length(0)) # 1 print(num_length(12345)) # 5 print(num_length(-12345)) # 5 print(num_length(9999999999999999999999999)) # 25 print(num_length(-9999999999999999999999999)) # 25

## Looping and Removing Digits Off the End

If you have a positive *or* a negative integer, you can do the following:

- Create a loop, and remove the last digit from the number in each iteration till there are
*no*digits left; - In each iteration, increment a counter, which would give the total number of digits in the number.

def num_length(num): num = abs(num) length = 0 while True: length += 1 num = num // 10 if num == 0: break return length print(num_length(0)) # 1 print(num_length(12345)) # 5 print(num_length(-12345)) # 5 print(num_length(9999999999999999999999999)) # 25 print(num_length(-9999999999999999999999999)) # 25

## Calculating the Number of Digits

You should use this method with caution as it may give you the wrong result for really large numbers (such as `9999999999999999999999999`

).

If you have a positive integer, you can do the following:

- Calculate the
`log10`

of the number, convert it to an integer and add`1`

to the result; - If the number is
`0`

, then return`1`

as a count (because`log10(0)`

equals`-inf`

).

import math num = 12345 length = 1 if num == 0 else int(math.log10(num)) + 1 print(length) #=> 5

If the number can potentially be negative, then you *must* first get the absolute value of the integer:

import math num = -12345 length = 1 if num == 0 else int(math.log10(abs(num))) + 1 print(length) #=> 5

To make it reusable, you could make this into a function, for example, like so:

import math def num_length(num): return 1 if num == 0 else int(math.log10(abs(num))) + 1 print(num_length(0)) # 1 print(num_length(12345)) # 5 print(num_length(-12345)) # 5 // this may give wrong result for really large numbers print(num_length(9999999999999999999999999)) # 26 print(num_length(-9999999999999999999999999)) # 26

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.