Tutorials Home   >   Programming Basics   >   Input & Output

Input & Output

What is Input & Output?

In programming, Input and Output (I/O) refer to the communication between a program and the outside world, which can be the user, files, or other programs.

  • Input: Data received by the program from the user or another source.

  • Output: Data sent from the program to the user, a file, or another program.

Think of it as a conversation: the program listens (input) and responds (output).

For example, if a program asks for your name and prints a greeting, the name you enter is input, and the greeting is output.


Input in Programming

Input allows users to provide data to the program during execution. Different programming languages have different ways to take input.

a) Input in Python

In Python, the input() function is used to read data from the user. By default, the input is read as a string.

name = input("Enter your name: ")
print("Hello, " + name + "!")

Output:

Enter your name: Alice
Hello, Alice!

If you want to take numbers as input, you need to convert the type:

age = int(input("Enter your age: "))
print("You are", age, "years old.")

b) Input in Java

In Java, the Scanner class is used to take input:

import java.util.Scanner;

public class InputExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.print(“Enter your name: “);
String name = sc.nextLine();

System.out.println(“Hello, “ + name + “!”);
}
}


Output in Programming

Output allows the program to communicate results to the user or another system.

a) Output in Python

Python uses the print() function to display output:

print("Hello, World!")

You can also print variables:

name = "Alice"
age = 20
print(name, "is", age, "years old.")

Output:

Alice is 20 years old.

b) Output in Java

In Java, the System.out.println() or System.out.print() methods are used:

System.out.println("Hello, World!"); // prints with a new line
System.out.print("Hello"); // prints without a new line
System.out.print(" World!");

Output:

Hello World!

Types of Input & Output

  1. Console I/O – Input and output through the terminal or console.
    Example: Using input() in Python or Scanner in Java.

  2. File I/O – Reading from and writing to files.

Python Example:

# Writing to a file
with open("example.txt", "w") as file:
file.write("Hello, File!")
# Reading from a file
with open(“example.txt”, “r”) as file:
content = file.read()
print(content)

Java Example:

import java.io.*;

public class FileIOExample {
public static void main(String[] args) throws IOException {
// Writing to a file
FileWriter writer = new FileWriter(“example.txt”);
writer.write(“Hello, File!”);
writer.close();

// Reading from a file
BufferedReader reader = new BufferedReader(new FileReader(“example.txt”));
String line = reader.readLine();
System.out.println(line);
reader.close();
}
}


Formatting Input & Output

Programmers often need formatted input and output to make programs readable and user-friendly.

a) Formatting Output in Python

name = "Alice"
age = 20
print(f"{name} is {age} years old.") # Using f-string

Output:

Alice is 20 years old.

You can also align text and control decimal points:

pi = 3.14159
print(f"Value of pi: {pi:.2f}") # rounds to 2 decimal places

Output:

Value of pi: 3.14

b) Formatting Output in Java

String name = "Alice";
int age = 20;
System.out.printf("%s is %d years old.\n", name, age); // printf for formatting

Output:

Alice is 20 years old.

Β Best Practices for Input & Output

  1. Validate Input – Always check if user input is correct to avoid errors.

age = int(input("Enter age: "))
if age < 0:
print("Age cannot be negative.")
  1. Use Clear Prompts – Make it obvious what input is expected.

name = input("Enter your full name: ")
  1. Handle Exceptions – Anticipate errors like invalid data or file not found.

try:
num = int(input("Enter a number: "))
except ValueError:
print("Invalid input! Please enter a number.")
  1. Format Output for Readability – Proper spacing, alignment, and labels help users understand the output.


Summary

Input and Output are essential for interaction between the program and the user or other systems.

  • Input allows the program to receive data from the user, a file, or another program.

  • Output allows the program to send results to the user, a file, or another program.

  • Console I/O and File I/O are the most common types.

  • Formatting and validation make I/O user-friendly and robust.

Understanding Input and Output is a fundamental skill for every programmer, as it is the bridge between the program and the real world.