Introduction to Output Formatting in Python

Introduction to Output Formatting in Python

When we write programs, we often display information on the screen using the print() function. But raw output is not always neat or easy to read. That is where formatting comes in.

Formatting simply means:

Controlling how the output looks when printed.

For example, arranging words in columns, aligning numbers, limiting decimal places, or inserting values into messages.

Why Do We Need Formatting?

Imagine printing student ages like this:

Krishna 90
Ram 80
Bhairava 100        

The names have different lengths, so the output looks messy. Formatting helps us make it look neat:

Krishna      90
Ram          80
Bhairava     100        

Types of Output Formatting in Python

Python provides three main ways to format output:

  1. Percentage (%) formatting — the oldest style
  2. str.format() method — more flexible
  3. f-strings — modern, simple, and recommended

1. Percentage (%) Formatting

This is the oldest method, similar to how formatting works in C language.

Example

name = "Krishna"
age = 25
print("Name: %s, Age: %d" % (name, age))        

Output

Name: Krishna, Age: 25        

Meaning

  • %s → placeholder for string
  • %d → placeholder for integer
  • Python inserts name and age into the message.

Percentage formatting is simple but limited. Still useful for very basic output.

2. Using str.format()

This method is more powerful. We write placeholders {} in the string.

Basic Example

name = "Krishna"
age = 25
print("Name: {}, Age: {}".format(name, age))        

Positional formatting

print("{1} is {0} years old".format(25, "Krishna"))        

Named placeholders

print("Name: {n}, Age: {a}".format(n="Krishna", a=25))        

Formatting in columns

print("{:<10} {:<5}".format("Name", "Age"))
print("{:<10} {:<5}".format("Krishna", 25))        

:<10 means left align within 10 spaces.

This method is very useful for creating neat tables.

The general pattern is:

{value:alignment width}        

Where:

  • alignment → <, >, ^
  • width → total number of spaces reserved

a) Left Alignment — <

Text starts from the left and the remaining width is filled on the right.

Example

print("{:<10}".format("Hello"))        

Output (dots shown for clarity)

Hello..... (total width = 10)

b) Right Alignment — >

Text is pushed to the right and the empty spaces are on the left.

Example

print("{:>10}".format("Hello"))        

Output

.....Hello

c) Center Alignment — ^

Text is placed in the middle, with spaces on both sides.

Example

print("{:^10}".format("Hello"))        

Output

..Hello..        

(Spaces distributed on left and right)

d) Fully Justified Alignment (special case)

Python does not have a built-in full justification like MS Word (where words stretch to fill space).

But we can create left and right justification using formatting.

Full justification (stretching text to both ends) requires writing logic manually — used rarely.

Article content
Summary Table

3. f-Strings (Formatted String Literals)

Introduced in Python 3.6, f-strings are the newest and simplest way. Start the string with f and insert variables inside {}.

Basic Example

name = "Krishna"
age = 25
print(f"Name: {name}, Age: {age}")        

Doing calculations inside

print(f"5 + 3 = {5 + 3}")        

Controlling decimal places

pi = 3.14159
print(f"{pi:.2f}")   # 3.14        

Aligning output

print(f"{'Krishna':<10}{25:<5}")        

f-strings are clean, fast, and widely used today.

Practice for Beginners

Try these simple tasks:

1. Print a message using each method:

My name is <your name> and I am <your age> years old.        

2. Print the following table neatly:

Name       Marks
Manu       85
Rita       92
Somu       78        

3. Print pi rounded to 3 decimal places using f-strings.

Final Thought

Formatting is one of the simplest but most powerful skills in Python. As you move to file handling, databases, reporting, or data science, clean output becomes extremely important.

This article gives you the foundation and from here, you can format anything!

Happy Coding!

To view or add a comment, sign in

More articles by Swarooprani Manoor

Others also viewed

Explore content categories