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:
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:
John
How input() Works
When Python encounters the input() function:
- It displays the prompt message.
- Waits for user input.
- Stores the entered value.
- Returns the value as a string.
Example:
city = input("Enter your city: ")
print(city)
Output:
Delhi
Python Output
The print() function is used to display information on the screen.
Syntax:
print(object)
Example:
print("Welcome to Python")
Output:
Displaying Variables
Example:
name = "John"
print(name)
Output:
Displaying Multiple Values
Example:
name = "John"
age = 25
print(name, age)
Output:
Taking Numeric Input
The input() function always returns a string.
Example:
age = input("Enter your age: ")
print(type(age))
Output:
<class ‘str’>
To perform calculations, convert the input into an integer.
Example:
age = int(input("Enter your age: "))
print(age + 5)
Output:
30
Taking Float Input
Example:
price = float(input("Enter price: "))
print(price)
Output:
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:
John
25
Formatted Output
Python provides several ways to format output.
Using Commas
Example:
name = "John"
age = 25
print("Name:", name, "Age:", age)
Output:
Using f-Strings
Example:
name = "John"
age = 25
print(f"My name is {name} and I am {age} years old.")
Output:
Using format()
Example:
name = "John"
print("Welcome {}".format(name))
Output:
Using sep Parameter
The sep parameter changes the separator between values.
Example:
print("Python", "Java", "C++", sep="-")
Output:
Using end Parameter
The end parameter changes the default line ending.
Example:
print("Hello", end=" ")
print("Python")
Output:
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:
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 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 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:
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 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:
Correct:
age = int(input("Enter age: "))
print(age + 5)
2. Assuming input() Returns Numbers
Example:
number = input("Enter number: ")
print(type(number))
Output:
input() always returns a string.
3. Missing Quotes in print()
Incorrect:
print(Hello Python)
Output:
Correct:
print("Hello Python")
4. Incorrect split() Usage
Incorrect:
name, age = input().split()
If the user enters only one value:
John
Output:
Always ensure the correct number of values are entered.
5. Using Undefined Variables
Incorrect:
print(username)
Output:
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?
Q 2: Which function is used to display output?
Q 3: What type of data does input() return?
Q 4: How can input be converted into a number?
Q 5: Can multiple values be printed at once?
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?