In Python 2 (and Python 3) you can do:
number = 1 print("%02d" % (number,))
Basically % is like printf
or sprintf
(see docs).
For Python 3.+, the same behavior can also be achieved with format
:
number = 1 print("{:02d}".format(number))
For Python 3.6+ the same behavior can be achieved with f-strings:
number = 1 print(f"{number:02d}")
Related Posts:
- How do you round UP a number?
- How do you round UP a number?
- Maximum and Minimum values for ints
- Maximum and Minimum values for ints
- What does %s mean in a python format string?
- How do I parse a string to a float or int?
- Round a floating-point number down to the nearest integer?
- What does %s mean in a python format string?
- Maximum and Minimum values for ints
- What’s the difference between %s and %d in Python string formatting?
- How can I convert a character to a integer in Python, and viceversa?
- What’s the difference between %s and %d in Python string formatting?
- Convert hex to binary
- Converting binary to decimal integer output
- Checking whether a variable is an integer or not
- Python, print all floats to 2 decimal places in output
- TypeError: unsupported format string passed to list.__format__
- Python TypeError: non-empty format string passed to object.__format__
- ValueError: cannot switch from manual field specification to automatic field numbering
- Converting integer to binary in python
- What is the purpose of dollar sign in this Python3 string formatting expression?
- Convert floats to ints in Pandas?
- How to find length of digits in an integer?
- When to use %r instead of %s in Python? [duplicate]
- How to iterate over rows in a DataFrame in Pandas
- How to convert list to string [duplicate]
- How to uninstall a package installed with pip install –user
- Converting integer to string in Python
- numpy max vs amax vs maximum
- TypeError: ‘str’ object is not callable (Python)
- ModuleNotFoundError: No module named ‘sklearn’
- Does Python have an ordered set?
- Asking the user for input until they give a valid response
- Print a list in reverse order with range()?
- Configuring so that pip install can work from github
- Proper way to declare custom exceptions in modern Python?
- TypeError: ‘float’ object not iterable
- write() versus writelines() and concatenated strings
- Why is it string.join(list) instead of list.join(string)?
- Importing class from another file [duplicate]
- Numpy/Python Array Value error
- Changing the “tick frequency” on x or y axis in matplotlib?
- TypeError: unsupported operand type(s) for -: ‘str’ and ‘str’
- Remove all special characters, punctuation and spaces from string
- AttributeError: ” object has no attribute ”
- Plot pie chart and table of pandas dataframe
- ModuleNotFoundError: No module named ‘matplotlib’
- “inconsistent use of tabs and spaces in indentation”
- Pandas ‘count(distinct)’ equivalent
- How can I flush the output of the print function?
- Python calling method in class
- No module named pkg_resources
- Opening a .ipynb.txt File
- TypeError: list indices must be integers or slices, not str
- python object() takes no parameters error
- How do I find the distance between two points?
- How do I calculate percentiles with python/numpy?
- How do I remove a substring from the end of a string?
- Selecting with complex criteria from pandas.DataFrame
- How can I download Anaconda for python 3.6
- django:django.core.exceptions.AppRegistryNotReady: Apps aren’t loaded yet
- Replace and overwrite instead of appending
- module ‘pandas’ has no attribute ‘rolling_mean’
- Using __add__ operator with multiple arguments in Python
- How to constantly run Python script in the background on Windows?
- Django – “no module named django.core.management”
- Type error Unhashable type:set
- Saving a Numpy array as an image
- Python: ValueError: Mixing iteration and read methods would lose data
- python: restarting a loop
- “for loop” with two variables?
- Decode Hex String in Python 3
- Python ImportError: No module named wx
- Accessing a class’ member variables in Python?
- Declaring a multi dimensional dictionary in python
- How do I compute derivative using Numpy?
- No module named utils error on compiling py file
- Convert Pandas Column to DateTime
- ImportError: cannot import name NUMPY_MKL
- Disable / Enable Button in TKinter
- Python: Pandas pd.read_excel giving ImportError: Install xlrd >= 0.9.0 for Excel support
- Meaning of list[-1] in Python
- Tkinter error: Couldn’t recognize data in image file
- How to install Openpyxl with pip
- getting ZeroDivisionError: integer division or modulo by zero
- Iterating through list of list in Python
- Installing scipy for python 2.7
- Installing MySQL-python causes command ‘clang’ failed with exit status 1 on Mac OS 10.13.15
- Get difference between two lists
- how to build .exe for python 3.5+, 3.6 if possible?
- python dictionary error AttributeError: ‘list’ object has no attribute ‘keys’
- What is a mixin, and why are they useful?
- What is the most efficient way to store a list in the Django models?
- Convert list of dictionaries to a pandas DataFrame
- How to read html from a url in python 3
- Set variable in jinja
- Python how to write to a binary file?
- How to download a file via FTP with Python ftplib
- How do I restart a program based on user input?
- Python Pandas : pivot table with aggfunc = count unique distinct