Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python counting the digits of a number

Python counting the digits of a number

Document Object Reference Manual Python3 Examples

We can easily determine the number of digits of a number by converting it to a string and then counting the length of the string. This method is simple and intuitive.

Example

def

count_digits

(

number

)

:

   

return

len

(

str

(

number

)

)

Example usage

number

=

12345

print

(

f

"The number {number} has {count_digits(number)} digits."

)

Code analysis:

Output result:

The number 12345 has 5 digits.

Document Object Reference Manual Python3 Examples