Understanding Python Formatted Output
In Python, the print() function can do more than just display text. With format specifiers, you can control how values appear — be it a string, integer, float, or character. This makes your output readable and well-structured.
Let’s begin with a simple example. Suppose we want to display the population of a country. Here’s how we can do it:
country = "India"
population = 140
print("Population of %s is %d crores" % (country, population))
How This Works:
- %s is a placeholder for a string
- %d is a placeholder for an integer
- Variables are passed after the % symbol in a tuple
- Output: the placeholders get replaced by the actual values
This is called old-style formatting, and it’s still widely used for its simplicity.
Here’s a quick reference for formatting characters:
- %s – String
- %d – Integer
- %f – Float (default shows 6 decimal places)
- %c – Character
Advanced Formatting Techniques
Let’s move on to a slightly more detailed example. Assume we want to print a person's name, age, and height:
name = "Rajesh"
age = 45
height = 5.6
print("Mr. %s is %d years old, his height is %.2f feet." % (name, age, height))
Why %.2f?
By default, %f displays six decimal places (like 5.600000). To limit it to two, use %.2f. This tells Python to round the float to two decimal places.
If you want to show no decimals, use %.0f, and Python will round to the nearest whole number.
print("Height is %.0f feet" % height)
This would output: Height is 6 feet
Justification and Width Formatting
Suppose you want numbers to appear neatly in columns. You can control the total width and alignment:
- %10d – right-aligns an integer in a field of 10 characters
- %-10d – left-aligns the integer
- %7.2f – total width is 7, with 2 decimals (e.g., 12.34)
Let’s take a float value 2.683:
value = 2.683
print("Formatted value: %7.2f" % value)
Explanation:
- Total width: 7
- Decimal digits: 2
- Output: 2.68 (note the space padding)
Summary of Formatting Options:
Format | Description |
---|---|
%s | String |
%d | Integer |
%f | Float (default 6 decimal places) |
%.2f | Float with 2 decimal places |
%10d | Right-align integer (10 width) |
%-10d | Left-align integer (10 width) |
%7.2f | Float: total 7 width, 2 decimals |
Remember, order matters when you pass multiple variables. They are substituted into the format string in the same order they appear in the tuple.
Sample Code: Using Multiple Format Specifiers
Explanation of Code:
- "Employee: %s" formats a string (name)
- "Age: %d years" uses an integer formatter
- "Height: %.2f feet" formats a float to 2 decimals
- Salary: %10d INR" right-aligns the salary in a 10-character width
Output
Employee: Rajesh
Age: 45 years
Height: 5.68 feet
Salary: 120000 INR
Note how salary is aligned to the right, and height is rounded to two decimal places.
Additional Topic: New-Style String Formatting with .format()
While the % method is still widely used, Python introduced a new style using .format():
print("Mr. {} is {} years old and {:.2f} feet tall.".format(name, age, height))
Here:
- {} are placeholders
- {:d}, {:s}, :.2f can specify types or formats
- More readable and less error-prone with named placeholders
Example:
print("Name: {n}, Age: {a}, Height: {h:.1f}".format(n=name, a=age, h=height))
This method is more flexible and recommended for modern Python code.
Watch the Full Tutorial on YouTube
We’ve covered a lot here, but formatted output is best understood through practice. For a step-by-step visual walkthrough, check out our full YouTube video: Watch the video on Formatted Output in Python
Conclusion
Formatted output makes your Python programs more professional and easier to read. Whether you’re printing strings, integers, or floats, learning how to format your output properly will save you time and confusion later. Keep practicing and experimenting with different format options—you’ll master it quickly!