newtum

Our Social Network

PYTHON FORMATTED OUTPUT: MASTERING PRINT() WITH STYLE

Python Formatted Output: Mastering print() with Style

When working with data in Python, printing it neatly and clearly is a basic yet crucial task. Whether you're showing results, debugging, or displaying user information, Python's formatted output can make your programs cleaner and more professional. In this blog, we’ll explore how to use the print() function effectively with formatting, placeholders, and alignment to control how your output looks.

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

1name = "Rajesh"
2age = 45
3height = 5.6789
4salary = 120000
5
6# Using % formatting
7print("Employee: %s" % name)
8print("Age: %d years" % age)
9print("Height: %.2f feet" % height)
10print("Salary: %10d INR" % salary)

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!

Formatted output is not just about looks—it reflects the quality and readability of your code.

— Manoj Kolhe

More Articles

Ready to Explore the Future of Technology?

Unlock the tools and insights you need to thrive on social media with Newtum. Join our community for expert tips, trending strategies, and resources that empower you to stand out and succeed.

Newtum

Newtum is an emerging online academy offering a wide range of high-end technical courses for people of all ages. We offer courses based on the latest demands and future of the IT industry.

© Newtum, Inc. All Rights Reserved.