Introduction

Input and Output (I/O) are the basic operations performed in every Python program.

  • Input is the data entered by the user.
  • Output is the information displayed to the user.

Python provides built-in functions such as input() and print() to perform input and output operations.


Input in Python

Definition

The input() function is used to accept data from the user through the keyboard.

Syntax

variable = input("Enter a value: ")
  • input() always returns the entered value as a string.

Example 1: Taking String Input

name = input("Enter your name: ")

print("Welcome", name)

Output

Enter your name: Alice
Welcome Alice

Example 2: Taking Integer Input

Since input() returns a string, use int() to convert it into an integer.

age = int(input("Enter your age: "))

print("Your age is:", age)

Output

Enter your age: 20
Your age is: 20

Example 3: Taking Float Input

salary = float(input("Enter your salary: "))

print("Salary =", salary)

Output

Enter your salary: 25000.50
Salary = 25000.5

Output in Python

Definition

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

Syntax

print(value)

Example 1: Printing Text

print("Hello, World!")

Output

Hello, World!

Example 2: Printing Variables

name = "Rahul"
age = 21

print(name)
print(age)

Output

Rahul
21

Example 3: Printing Multiple Values

name = "Rahul"
age = 21

print("Name:", name, "Age:", age)

Output

Name: Rahul Age: 21

Input and Output Together

Example: Addition of Two Numbers

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

sum = a + b

print("Sum =", sum)

Output

Enter first number: 15
Enter second number: 20
Sum = 35

Type Conversion with Input

Python provides functions to convert input values into different data types.

FunctionDescriptionExample
int()Converts to integerint(input())
float()Converts to floatfloat(input())
str()Converts to stringstr(100)
bool()Converts to Booleanbool(1) โ†’ True

Formatting Output

Using f-Strings (Recommended)

name = "Anita"
marks = 92

print(f"{name} scored {marks} marks.")

Output

Anita scored 92 marks.

Using format()

name = "Anita"
marks = 92

print("{} scored {} marks.".format(name, marks))

Output

Anita scored 92 marks.

Escape Characters in Output

Escape CharacterDescription
\nNew line
\tHorizontal tab
\\Backslash (\)
\"Double quotation mark

Example

print("Name\tAge")
print("Rahul\t21")

Output

Name    Age
Rahul   21

Advantages of Input and Output

  • Makes programs interactive.
  • Accepts data from users during execution.
  • Displays results in a readable format.
  • Supports different data types.
  • Improves user experience.