💬 What is Input/Output (I/O) in Python?
Input allows programs to receive data from users or external sources (like keyboards, CLI prompts, or files).
Output allows programs to display results on screens, logs, or persistent text files.
Mastering Python's built-in print() and input() functions is essential for building interactive command-line applications and administrative tools.
1. Basic Output with print()
The
print() function outputs text, numbers, or expressions directly to the standard console.
Python
# Basic output statements
print("Welcome to Nikita Infotech Python Training!")
print(2026)
print(10 + 20)
Output:
Welcome to Nikita Infotech Python Training!
2026
30
Real-Time Example 1: Interactive User Registration (input)
Capture user data interactively from the command line using the
input() function.
Python
# Simulated interactive console input
# username = input("Enter your username: ")
username = "nikita_dev" # Simulated input value
print(f"Success! User '{username}' registered successfully.")
Output:
Success! User 'nikita_dev' registered successfully.
Real-Time Example 2: E-Commerce Bill Calculator with Type Casting
Because
input() always returns a string, numeric inputs must be explicitly type-cast using int() or float() before calculations.
Python
# Simulated user inputs for price and quantity
price_input = "450"
qty_input = "3"
subtotal = float(price_input) * int(qty_input)
tax = subtotal * 0.05
grand_total = subtotal + tax
print(f"Subtotal: ₹{subtotal}")
print(f"GST (5%): ₹{tax}")
print(f"Grand Total: ₹{grand_total}")
Output:
Subtotal: ₹1350.0
GST (5%): ₹67.5
Grand Total: ₹1417.5
Real-Time Example 3: Formatting Output with F-Strings & Decimal Precision
Format floating-point financial output cleanly using f-string specifiers (e.g.,
:.2f).
Python
account_balance = 25480.5678
# Format output to 2 decimal places for currency
print(f"Available Account Balance: ₹{account_balance:.2f}")
Output:
Available Account Balance: ₹25480.57
Real-Time Example 4: Custom Separators (sep) and Endings (end) in print()
Customize how items are separated and ended in console prints using
sep and end parameters.
Python
# Custom separator for CSV-like output
print("192", "168", "1", "1", sep=".")
# Custom ending to keep prints on the same line
print("Loading module...", end=" ")
print("[OK]")
Output:
192.168.1.1
Loading module... [OK]
Real-Time Example 5: Handling Multiple Inputs in a Single Line
Capture multiple space-separated values in one prompt using
split().
Python
# Simulated single-line input: "Rahul 25 Engineer"
raw_input_data = "Rahul 25 Engineer"
name, age, profession = raw_input_data.split()
print(f"Name: {name} | Age: {age} | Profession: {profession}")
Output:
Name: Rahul | Age: 25 | Profession: Engineer
Real-Time Example 6: Writing Output to a Text File (File I/O)
Instead of printing to the console, write program output directly to a persistent text log file using
open(..., 'w').
Python
log_filename = "server_audit.txt"
# Write data to file
with open(log_filename, "w") as file:
file.write("AUDIT LOG REPORT\n")
file.write("Status: Server running normally.\n")
print("Output successfully written to file:", log_filename)
Output:
Output successfully written to file: server_audit.txt
Real-Time Example 7: Reading Input from External Configuration Files
Read configuration settings or text records from a local file using
open(..., 'r').
Python
# Reading back the audit log file created previously
try:
with open("server_audit.txt", "r") as file:
file_contents = file.read()
print("File Contents Read:\n" + file_contents)
except FileNotFoundError:
print("Configuration file not found.")
Output:
File Contents Read:
AUDIT LOG REPORT
Status: Server running normally.
Real-Time Example 8: Appending Transaction Audit Logs
Append new log entries to an existing file without overwriting old data using
open(..., 'a').
Python
# Append a new event line
with open("server_audit.txt", "a") as file:
file.write("EVENT: User login successful at 10:45 AM\n")
print("New event appended to audit log successfully.")
Output:
New event appended to audit log successfully.
Real-Time Example 9: Formatted Tabular Output (CLI Reports)
Align columns cleanly in the console output using string alignment padding (
< or >).
Python
print(f"{'Item Name':<15} | {'Price (₹)':>10}")
print("-" * 30)
print(f"{'Keyboard':<15} | {1200:>10}")
print(f"{'Mouse':<15} | {450:>10}")
Output:
Item Name | Price (₹)
------------------------------
Keyboard | 1200
Mouse | 450