Different Input and Output Techniques in Python3

Input and Output are essential operations in programming that allow users to interact with programs. Input refers to data provided to the program from external sources, while output is how we display processed results. Python offers various techniques for handling input and output operations.

Input Techniques

Python provides several methods to receive input data from different sources.

Standard Input

Standard input captures user data through the keyboard using the input() function ?

name = input("Enter your name: ")
age = input("Enter your age: ")
print(f"Hello {name}, you are {age} years old!")
Enter your name: John
Enter your age: 25
Hello John, you are 25 years old!

Command Line Arguments

Command line arguments allow passing input parameters when executing a Python script using the sys module ?

import sys

print('Number of arguments:', len(sys.argv))
print('Script name:', sys.argv[0])

if len(sys.argv) > 1:
    print('Arguments:', sys.argv[1:])
else:
    print('No arguments provided')

Execute with: python script.py arg1 arg2 arg3

Number of arguments: 4
Script name: script.py
Arguments: ['arg1', 'arg2', 'arg3']

File Input

Reading input data from files using the open() function ?

# Create a sample file first
with open('sample.txt', 'w') as f:
    f.write("Python\n")
    f.write("Programming\n")
    f.write("Tutorial\n")

# Read from the file
with open('sample.txt', 'r') as f:
    content = f.read()
    print("File content:")
    print(content)
File content:
Python
Programming
Tutorial

Output Techniques

Python offers multiple ways to display processed data and results.

Standard Output

The print() function displays output to the console ?

message = "Welcome to Tutorialspoint"
number = 42

print("Simple output:", message)
print("Number:", number)
print("Multiple values:", message, number)
Simple output: Welcome to Tutorialspoint
Number: 42
Multiple values: Welcome to Tutorialspoint 42

File Output

Writing output data to files for storage or later use ?

data = ["Python", "Java", "JavaScript", "C++"]

with open('languages.txt', 'w') as f:
    f.write("Popular Programming Languages:\n")
    for language in data:
        f.write(f"- {language}\n")

print("Data written to languages.txt successfully")
Data written to languages.txt successfully

Formatted Output

Python provides several methods for formatting output strings ?

name = "Alice"
score = 95.5
subject = "Python"

# Using format() method
print("Student {} scored {:.1f}% in {}".format(name, score, subject))

# Using f-strings (recommended)
print(f"Student {name} scored {score:.1f}% in {subject}")

# Using % formatting (older method)
print("Student %s scored %.1f%% in %s" % (name, score, subject))
Student Alice scored 95.5% in Python
Student Alice scored 95.5% in Python
Student Alice scored 95.5% in Python

Comparison

Technique Input Method Output Method Best For
Standard I/O input() print() Interactive programs
File I/O open().read() open().write() Data persistence
Command Line sys.argv print() Script automation
Formatted Any method f-strings Structured output

Conclusion

Python offers flexible input/output techniques for different scenarios. Use input() for interactive programs, file I/O for data persistence, and command-line arguments for automation. F-strings provide the most readable formatting for output display.

Updated on: 2026-03-27T15:51:53+05:30

585 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements