How to dynamically zero pad in Python

Because it made me think a little.

If not dynamic

This is a well-introduced method and I think it's a basic grammar.

'{0:05d}'.format(12)  #=> '00012'

It has nothing to do with zero filling, but it looks like the following to fill with spaces.

'{0:5d}'.format(12)  #=> '   12'

When making it dynamic

In Python, you can't use variables in fields like {0: 05d}, so you can't "dynamically specify the number of digits to get a string" in this notation.

Therefore, it can be realized by the following method.

python


# -*- coding: utf-8 -*-

max = 10000  #<=Assuming this will be dynamic

#Get the maximum digit
max_len = len(str(max))
#Target numerical value (appropriate this time)
test = 123

###Zero padding with maximum digits for the target number###
# `zfill`When using
output = str(test).zfill(max_len)
print(output)  #=> '00123'

#When using rjust
output = str(test).rjust(max_len,'0')
print(output)  #=> '00123'

By using zfill, variables can be used for the number of digits specified by the argument.

Also, if you use rjust, you can fill it with non-zero characters by specifying a character other than 0 as the second argument. (@Matobaa san taught me how to use rjust. Thankx @matobaa san!)

Recommended Posts

How to dynamically zero pad in Python
How to dynamically define variables in Python
How to develop in Python
[Python] How to do PCA in Python
How to collect images in Python
How to use SQLite in Python
How to use Mysql in python
How to wrap C in Python
How to use PubChem in Python
How to handle Japanese in Python
[Introduction to Python] How to use class in Python?
How to access environment variables in Python
How to do R chartr () in Python
[Itertools.permutations] How to put permutations in Python
How to work with BigQuery in Python
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
How to check opencv version in python
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to use regular expressions in Python
How to display Hello world in python
How to use is and == in Python
How to write Ruby to_s in Python
How to install Python
How to install python
How to use the C library in Python
How to receive command line arguments in Python
[REAPER] How to play with Reascript in Python
How to clear tuples in a list (Python)
How to generate permutations in Python and C ++
How to embed a variable in a python string
How to implement Discord Slash Command in Python
Summary of how to import files in Python 3
How to simplify restricted polynomial fit in python
How to implement shared memory in Python (mmap.mmap)
How to create a JSON file in Python
To dynamically replace the next method in python
Summary of how to use MNIST in Python
How to specify TLS version in python requests
How to notify a Discord channel in Python
How to get the files in the [Python] folder
How to use tkinter with python in pyenv
How to run Leap Motion in non-Apple Python
[Python] How to draw a histogram in Matplotlib
How to output "Ketsumaimo" as standard output in Python
How to handle datetime type in python sqlite3
How to make Python Interpreter changes in Pycharm
How to plot autocorrelation and partial autocorrelation in python
How to remove duplicate elements in Python3 list
20th Offline Real-time How to Write Problems in Python
How to retrieve the nth largest value in Python
[2020.8 latest] How to install Python
How to get the variable name itself in python
How to install Python [Windows]
To flush stdout in Python
How to get the number of digits in Python
python3: How to use bottle (2)
How to write string concatenation in multiple lines in Python