MWZ

MINDWAREZONE

Output Formatting in python

When creating Python programs, it's important to display information in a clear and readable way. Output formatting helps you control how text, numbers, and variables appear on the screen.

Python provides several ways to format output, from simple printing to advanced string formatting techniques.

In this tutorial, you'll learn the most common methods of output formatting with easy-to-understand examples.

What is Output Formatting?

Output formatting is the process of displaying data in a specific and organized format.

For example:

name = "John" age = 25 
print("Name:", name) 
print("Age:", age)
            

Output:

Name: John 
Age: 25
            

Formatting makes the output easier to read and understand.

Using print() Function

The print() function is used to display output on the screen.

print("Hello, World!")
            

Output:

Hello, World!
            

Printing Multiple Values

You can print multiple values by separating them with commas.

name = "Alice" 
age = 22 
print("Name:", name, "Age:", age)
            

Output:

Name: Alice Age: 22
            

Using the sep Parameter

The sep parameter allows you to specify a separator between values.

print("Python", "Java", "C++", sep=" | ")
            

Output:

Python | Java | C++
            

Using the end Parameter

By default, print() moves to a new line after printing.

print("Hello") 
print("World")
            

Output:

Hello 
World
            

You can change this behavior using the end parameter.

print("Hello", end=" ") 
print("World")
            

Output:

Hello World
            

String Concatenation

You can combine strings using the + operator.

name = "John" 
print("Hello " + name)
            

Output:

Hello John
            

Using format() Method

The format() method inserts values into a string.

name = "Alice" 
age = 25 
print("My name is {} and I am {} years old.".format(name, age))
            

Output:

My name is Alice and I am 25 years old.
            

Using Positions

print("First: {0}, Second: {1}".format("Python", "Java"))
            

Output:

First: Python, Second: Java
            

Using f-Strings (Recommended)

F-strings are the modern and easiest way to format strings in Python.

name = "Alice" 
age = 25 
print(f"My name is {name} and I am {age} years old.")
            

Output

My name is Alice and I am 25 years old.
            

Why Use f-Strings?

  • Easy to read
  • Faster than older formatting methods
  • Recommended in modern Python code

Formatting Numbers

You can control how numbers are displayed.

Decimal Places

price = 99.98765 
print(f"{price:.2f}")
            

Output

99.99
            

The .2f displays the number with two decimal places.

Displaying Percentage Values

score = 0.85 print(f"{score:.0%}")
            

Output

85%
            

Practical Example

name = input("Enter your name: ") 
salary = float(input("Enter your salary: ")) 
print(f"Employee: {name}") 
print(f"Salary: ₹{salary:,.2f}")
            

Output

Employee: John 
Salary: ₹50,000.00
            

Common Formatting Methods

Method Example
Basic print print("Hello")
Separator print(a, b, sep="-")
End character print(a, end=" ")
format() "{}".format(value)
f-string f"{value}"
Decimal places f"{num:.2f}"
Comma separator f"{num:,}"