
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How can we do the basic print formatting for Python numbers?
You can format a floating number to the fixed width in Python using the format function on the string. For example,
nums = [0.555555555555, 1, 12.0542184, 5589.6654753] for x in nums: print("{:10.4f}".format(x))
This will give the output
0.5556 1.0000 12.0542 5589.6655
Using the same function, you can also format integers
nums = [5, 20, 500] for x in nums: print("{:d}".format(x))
This will give the output:
5 20 500
You can use it to provide padding as well, by specifying the number before d
nums = [5, 20, 500] for x in nums: print("{:4d}".format(x))
This will give the output
5 20 500
The https://pyformat.info/ website is a great resource to use for learning all nuances of formatting numbers in python.
- Related Articles
- How do we do variable formatting using the tag in HTML?
- How do we do computer output formatting using the tag in HTML?
- How can we print multiple blank lines in python?
- How can we generate Strong numbers in Python?
- How can we use complex numbers in Python?
- How to print a complete tuple in Python using string formatting?
- How can we combine multiple print statements per line in Python?
- How can we do Python operator overloading with multiple operands?
- How can we do date and time math in Python?
- How do we check in Python whether a string contains only numbers?
- How do we print a variable at the MongoDB command prompt?
- How Can We Do The Multiplication Of Monomials ?
- How can we do the multiplication of polynomials?
- Can we do math operation on Python Strings?
- In integer, when we have the same numbers and different sign then what we can do [+/-]?

Advertisements