Python Input and Output: input() and print() Examples

Introduction

Python provides simple and powerful built-in functions for handling input and output operations. The input() function is used to receive data from users, while the print() function is used to display information on the screen.

Understanding input and output is essential because it allows programs to interact with users, process information, and generate meaningful results.

What is Python Input and Output?

Python Input and Output refer to the process of receiving data from users and displaying results back to them.

Input

Input is the data provided by a user or another source to a program.

Output

Output is the information displayed by the program after processing the input.

Example:


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

Output:

Enter your name: John
Hello John

In this example:

  • input() receives the user’s name.
  • print() displays the greeting message.

Python Input

The input() function allows users to enter data during program execution.

Syntax:


input(prompt)

Explanation:

prompt: A message displayed to the user

Example:


name = input("Enter your name: ")
print(name)

Output:

Enter your name: John
John

How input() Works

When Python encounters the input() function:

  1. It displays the prompt message.
  2. Waits for user input.
  3. Stores the entered value.
  4. Returns the value as a string.

Example:


city = input("Enter your city: ")
print(city)

Output:

Enter your city: Delhi
Delhi

Python Output

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

Syntax:


print(object)

Example:


print("Welcome to Python")

Output:

Welcome to Python

Displaying Variables

Example:


name = "John"
print(name)

Output:

John

Displaying Multiple Values

Example:


name = "John"
age = 25
print(name, age)

Output:

John 25

Taking Numeric Input

The input() function always returns a string.

Example:


age = input("Enter your age: ")
print(type(age))

Output:

Enter your age: 25
<class ‘str’>

To perform calculations, convert the input into an integer.

Example:


age = int(input("Enter your age: "))
print(age + 5)

Output:

Enter your age: 25
30

Taking Float Input

Example:


price = float(input("Enter price: "))
print(price)

Output:

Enter price: 99.99
99.99

Taking Multiple Inputs

Python allows users to enter multiple values at once.

Example:


name, age = input("Enter name and age: ").split()
print(name)
print(age)

Output:

Enter name and age: John 25
John
25

Formatted Output

Python provides several ways to format output.

Using Commas

Example:


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

Output:

Name: John Age: 25

Using f-Strings

Example:


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

Output:

My name is John and I am 25 years old.

Using format()

Example:


name = "John"
print("Welcome {}".format(name))

Output:

Welcome John

Using sep Parameter

The sep parameter changes the separator between values.

Example:


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

Output:

Python-Java-C++

Using end Parameter

The end parameter changes the default line ending.

Example:


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

Output:

Hello Python

Escape Characters in Output

Escape characters provide special formatting.

Escape Character Description
\n New Line
\t Tab
Single Quote
Double Quote
\ Backslash

Example:


print("Hello\nPython")

Output:

Hello
Python

Example: Simple Calculator


num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
sum_result = num1 + num2
print("Sum =", sum_result)

Output:

Enter first number: 10
Enter second number: 20
Sum = 30

Example: User Registration


name = input("Enter your name: ")
email = input("Enter your email: ")
print("Registration Successful")
print("Name:", name)
print("Email:", email)

Output:

Enter your name: John
Enter your email: john@example.com
Registration Successful
Name: John
Email: john@example.com

Real-life Examples:

1. Online shopping application

Suppose you are developing an online shopping application where users enter product quantities.

Example:


quantity = int(input("Enter quantity: "))
total_price = quantity * 500
print("Total Price =", total_price)

Output:

Enter quantity: 3
Total Price = 1500

Explanation

  • The user enters the quantity.
  • The program calculates the total price.
  • The result is displayed using print().

This is a practical example of input and output operations used in real-world applications.

2. Student Marks Calculator


math_marks = int(input("Enter Math Marks: "))
science_marks = int(input("Enter Science Marks: "))
total = math_marks + science_marks
print("Total Marks =", total)

Output:

Enter Math Marks: 80
Enter Science Marks: 90
Total Marks = 170

This demonstrates how input and output help process user-provided data.

Advantages of Python Input and Output

1. User Interaction

Allows programs to communicate with users.

2. Dynamic Programs

Programs can accept different values during execution.

3. Easy Data Collection

Simplifies gathering information from users.

4. Better User Experience

Provides meaningful feedback and results.

5. Flexible Data Processing

Supports various data types and formats.

Common Mistakes

1. Forgetting Type Conversion

Incorrect:


age = input("Enter age: ")
print(age + 5)

Output:

TypeError

Correct:


age = int(input("Enter age: "))
print(age + 5)

2. Assuming input() Returns Numbers

Example:


number = input("Enter number: ")
print(type(number))

Output:

<class ‘str’>

input() always returns a string.

3. Missing Quotes in print()

Incorrect:


print(Hello Python)

Output:

SyntaxError

Correct:


print("Hello Python")

4. Incorrect split() Usage

Incorrect:


name, age = input().split()

If the user enters only one value:


John

Output:

ValueError

Always ensure the correct number of values are entered.

5. Using Undefined Variables

Incorrect:


print(username)

Output:

NameError

Define variables before using them.

Conclusion

Python Input and Output are essential concepts that enable programs to interact with users and display meaningful results. The input() function allows users to provide data during program execution, while the print() function displays information on the screen. Together, they form the foundation of interactive applications.

Python Input/Output – Interview Questions

Q 1: How do you take input from the user in Python?
Ans: Using the input() function.
Q 2: Which function is used to display output?
Ans: The print() function is used to display output.
Q 3: What type of data does input() return?
Ans: The input() function always returns data as a string.
Q 4: How can input be converted into a number?
Ans: By using type conversion functions like int() or float().
Q 5: Can multiple values be printed at once?
Ans: Yes, multiple values can be printed using commas in print().

Python Input/Output – Objective Questions (MCQs)

Q1. Which function is used to take input from the user in Python?






Q2. What will be the output of the following code if the user enters 5?

x = input("Enter a number: ")
print(x * 2)






Q3. How do you convert user input into an integer in Python?






Q4. Which of the following statements correctly prints output to the screen?






Q5. What is the default end character of the print() function in Python?






Related Python Tutorials