Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python int rightpad with 0

## Strings:

>>> n = '4'
>>> print(n.zfill(3))
004

## And for numbers:

>>> n = 4
>>> print(f'{n:03}') # Preferred method, python >= 3.6
004
>>> print('%03d' % n)
004
>>> print(format(n, '03')) # python >= 2.6
004
>>> print('{0:03d}'.format(n))  # python >= 2.6 + python 3
004
>>> print('{foo:03d}'.format(foo=n))  # python >= 2.6 + python 3
004
>>> print('{:03d}'.format(n))  # python >= 2.7 + python3
004


## Credit to "Harley Holcombe" on Stackoverflow
Comment

PREVIOUS NEXT
Code Example
Python :: create list 
Python :: ffmpeg python slow down frame rate 
Python :: django database specify schema 
Python :: python sys replace text 
Python :: ENUM AS STRING GODOT 
Python :: 4.3.3. Reassigning Variables 
Python :: install python3 yum centOS redhat 
Python :: create new model description odoo 
Python :: python laplace expansion 
Python :: # multithreading for optimal use of CPU 
Python :: # print random number 
Python :: pairplot legend position 
Python :: python regex type hint 
Python :: two lists with identical entries get order 
Python :: python case sensitive when dealing with identifiers 
Python :: aws chalice 
Python :: Simple Python Permutation Without Passing any argument 
Python :: int to floats 
Python :: python occ display point 
Python :: python replace every space, dash and parentheses into underscore 
Python :: python adding an item 
Python :: python typing optional argument 
Python :: Broadcasting with NumPy Arrays Plotting a two-dimensional function Example 
Python :: display colors in python console 
Python :: Python NumPy asfortranarray Function Tuple to an array 
Python :: objects list 
Python :: pytorch Jaccard Index 
Python :: NumPy rot90 Example Rotating Once 
Python :: NumPy bitwise_xor Code When inputs are Boolean 
Python :: using .get() for deep dictionary 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =