How to get the number of digits in Python

Introduction

How to get the number of digits. Casting the mold is a bit cumbersome but easy. [Addition] In the comment, I mentioned it because I was told how to do it smarter!

For integers

Convert a number to a string to get the length. If it is a negative number, the minus sign will be included in the number of digits, so convert it to an absolute value.

num = -12345
num = abs(num)           #Convert to absolute value
num_str = str(num)       #Convert to string
num_digits = len(num)    #Get the length (number of digits) of the character string

print(num_digits)        #Execution result 5

Or in one line

num = -12345
num_digits = len(str(abs(num)))     #Batch conversion
print(num_digits)                   #Execution result 5

For decimals

It is basically the same as the case of integers, but since it is the number of digits, the decimal point is excluded.

num = 0.123456
num = abs(num)
num_str = str(num).replace(".","")    #Ignore the decimal point
num_digits = len(num)
print(num_digits)                     #Execution result 7

Smart way

Count using isdigit () to determine if it is a number. Both integers and decimals can be written in this way.

def count_digit(value):
    return sum(c.isdigit() for c in str(value))

print(count_digit(-12345))     #Execution result 5
print(count_digit(0.12345))    #Execution result 6
print(count_digit(-3.14))      #Execution result 3

Recommended Posts

How to get the number of digits in Python
Get the number of digits
How to get the files in the [Python] folder
How to get the variable name itself in python
How to get the Python version
Get the size (number of elements) of UnionFind in Python
How to get a list of files in the same directory with python
How to find the optimal number of clusters in k-means
Get the number of specific elements in a python list
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
How to get a stacktrace in python
How to identify the element with the smallest number of characters in a Python list?
How to count the number of occurrences of each element in the list in Python with weight
How to determine the existence of a selenium element in Python
How to know the internal structure of an object in Python
How to check the memory size of a variable in Python
[Python] How to get the first and last days of the month
How to check the memory size of a dictionary in Python
How to get the vertex coordinates of a feature in ArcPy
Get the number of readers of a treatise on Mendeley in Python
How to use the C library in Python
Output the number of CPU cores in Python
Summary of how to import files in Python 3
Get the caller of a function in Python
Summary of how to use MNIST in Python
How to get dictionary type elements of Python 2.7
How to get the date and time difference in seconds with python
[Python] How to put any number of standard inputs in a list
[Python] Calculate the number of digits required when filling in 0s [Note]
How to develop in Python
Memo of the program to get the date in two digits with javascript, Ruby, Python, shell script
python beginners tried to predict the number of criminals
How to retrieve the nth largest value in Python
How to know the port number of the xinetd service
How to know the current directory in Python in Blender
[python] Get the list of classes defined in the module
[Python] Summary of how to specify the color of the figure
How to use the model learned in Lobe in Python
[Python] Get the number of views of all posted articles
Get the URL of the HTTP redirect destination in Python
[Python] How to output the list values in order
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
How to pass the execution result of a shell command in a list in Python
How to count the number of elements in Django and output to a template
Visualize the timeline of the number of issues on GitHub assigned to you in Python
How to find the coefficient of the trendline that passes through the vertices in Python
Note: How to get the last day of the month with python (added the first day of the month)
I want to get the file name, line number, and function name in Python 3.4
[Introduction to Python] How to get the index of data with a for statement
[Python] How to do PCA in Python
Get the number of views of Qiita
How to collect images in Python
Get the desktop path in Python
How to use SQLite in Python
Get the script path in Python
In the python command python points to python3.8
How to get started with Python
[Python] How to import the library
How to use Mysql in python
How to wrap C in Python