math.log10
Calculate the base-10 logarithm of a number.
Python 3.13
math.log10(x)- x
- A positive number.int | float
Return value
A float; raises ValueError for zero and negative numbers.
Examples
Base-10 logarithm
Python 3.13
import math
print(math.log10(100))2.0
Number of digits in a number
Python 3.13
import math
number = 12345
print(int(math.log10(number)) + 1)5
Details
It is more accurate than math.log(x, 10): math.log10(1000) gives exactly 3.0, and math.log(1000, 10) gives 2.9999999999999996. The integer part of the base-10 logarithm of a positive integer is one less than its number of digits, but only while float precision is enough, up to about 15 digits; len(str(n)) is reliable for any number.
