Input and Output Statements in Python
Complete Tutorial with Examples and Output
1. Introduction
Input and Output are fundamental concepts in every programming language. In Python, input() is mainly used to accept data from the user, while print() is used to display information on the screen.
A simple Python program generally follows this process:
For example, a program may accept two numbers from the user, calculate their sum, and display the result.
2. Output Statement – print()
The print() function is used to display text, numbers, variables,
expressions and other information on the screen.
Syntax
print(value)
Example
print("Hello Python")
Output
Hello Python
3. Printing Numbers
The print() function can display integer and floating-point values.
print(100)
print(25.75)
Output
100
25.75
4. Printing Variables
Variables can be directly passed to the print() function.
name = "Rahul"
age = 20
print(name)
print(age)
Output
Rahul
20
5. Printing Multiple Values
Multiple values can be passed to print() by separating them with commas.
name = "Rahul"
age = 20
marks = 85
print(name, age, marks)
Output
Rahul 20 85
6. Using sep in print()
The sep parameter specifies what should be placed between multiple
values printed by print().
print("2026", "09", "09", sep="-")
Output
2026-09-09
Another Example
print("Python", "Programming", "Language", sep=" | ")
Output
Python | Programming | Language
7. Using end in print()
Normally, print() moves to a new line after displaying the output.
The end parameter can change this behavior.
print("Hello", end=" ")
print("World")
Output
Hello World
Example 2
print("A", end="-")
print("B", end="-")
print("C")
Output
A-B-C
8. Escape Characters in Output
Escape characters are special characters used inside strings.
New Line – \n
print("Hello\nPython")
Output
Hello
Python
Tab – \t
print("Name\tMarks")
print("Rahul\t85")
Output
Name Marks
Rahul 85
9. String Concatenation in Output
The + operator can be used to join strings.
first = "Hello"
second = "Python"
print(first + " " + second)
Output
Hello Python
10. Formatted Output using f-strings
An f-string allows variables and expressions to be inserted directly inside a string.
name = "Rahul"
age = 20
marks = 85
print(f"Name: {name}")
print(f"Age: {age}")
print(f"Marks: {marks}")
Output
Name: Rahul
Age: 20
Marks: 85
11. Input Statement – input()
The input() function is used to accept data from the user through
the keyboard.
Syntax
variable = input("Message")
Example
name = input("Enter your name: ")
print("Hello", name)
Sample Output
Enter your name: Rahul
Hello Rahul
12. Reading a String from User
By default, the input() function returns the entered value as a string.
city = input("Enter your city: ")
print("You live in", city)
Sample Output
Enter your city: Kolkata
You live in Kolkata
13. Taking Integer Input
If the user enters a number that should be treated as an integer, use
int() to convert the input.
age = int(input("Enter your age: "))
print("Your age is", age)
Sample Output
Enter your age: 20
Your age is 20
14. Taking Floating-Point Input
Use float() when the input may contain a decimal value.
price = float(input("Enter price: "))
print("Price =", price)
Sample Output
Enter price: 99.50
Price = 99.5
15. Taking Multiple Inputs
Multiple values can be accepted from one line using split().
a, b = input("Enter two numbers: ").split()
print("First:", a)
print("Second:", b)
Sample Output
Enter two numbers: 10 20
First: 10
Second: 20
16. Taking Multiple Integer Inputs
The map() function can be combined with int() and
split() to read multiple integers.
a, b = map(int, input("Enter two numbers: ").split())
print("Sum =", a + b)
Sample Output
Enter two numbers: 10 20
Sum = 30
17. Taking Three Inputs
a, b, c = map(int, input("Enter three numbers: ").split())
print("Total =", a + b + c)
Sample Output
Enter three numbers: 10 20 30
Total = 60
18. Input and Output – Addition Example
This example demonstrates the complete input-processing-output process.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
sum_value = a + b
print("Sum =", sum_value)
Sample Output
Enter first number: 25
Enter second number: 15
Sum = 40
19. Practical Example – Area of Rectangle
length = float(input("Enter length: "))
width = float(input("Enter width: "))
area = length * width
print("Area of rectangle =", area)
Sample Output
Enter length: 10
Enter width: 5
Area of rectangle = 50.0
20. Practical Example – Student Marks
name = input("Enter student name: ")
marks = int(input("Enter marks: "))
print("Student Name:", name)
print("Marks:", marks)
Sample Output
Enter student name: Rahul
Enter marks: 85
Student Name: Rahul
Marks: 85
21. Practical Example – Total and Average Marks
name = input("Enter student name: ")
m1, m2, m3 = map(
int,
input("Enter marks of three subjects: ").split()
)
total = m1 + m2 + m3
average = total / 3
print(f"Student: {name}")
print(f"Total Marks: {total}")
print(f"Average: {average:.2f}")
Sample Output
Enter student name: Rahul
Enter marks of three subjects: 80 75 90
Student: Rahul
Total Marks: 245
Average: 81.67
22. Boolean Input
The input() function itself returns a string, so Boolean values need
to be handled explicitly.
For example, a simple conversion can be performed using a comparison:
answer = input("Are you a student? ")
is_student = answer.lower() == "yes"
print("Student:", is_student)
Sample Output
Are you a student? yes
Student: True
23. Checking the Type of Input
Since input() returns a string, the type can be checked using
type().
value = input("Enter something: ")
print(value)
print(type(value))
Sample Output
Enter something: 100
100
<class 'str'>
100, input()
initially returns it as a string.
24. Input Type Conversion
| Function | Purpose | Example |
|---|---|---|
str() |
Convert to string | str(100) |
int() |
Convert to integer | int("100") |
float() |
Convert to floating point | float("10.5") |
bool() |
Convert to Boolean | bool(1) |
25. Formatting Numerical Output
Formatted output is useful when displaying decimal values such as averages, percentages and prices.
average = 81.67895
print(f"Average = {average:.2f}")
Output
Average = 81.68
26. Displaying a Simple Table
print("Name\tMarks")
print("Rahul\t85")
print("Amit\t90")
print("Riya\t78")
Output
Name Marks
Rahul 85
Amit 90
Riya 78
27. Complete Input-Processing-Output Program
The following example demonstrates a complete Python program that accepts student information, calculates the total and average marks, and displays the result.
name = input("Enter student name: ")
m1, m2, m3 = map(
int,
input("Enter marks of three subjects: ").split()
)
total = m1 + m2 + m3
average = total / 3
print("\n----- STUDENT RESULT -----")
print(f"Name: {name}")
print(f"Subject 1: {m1}")
print(f"Subject 2: {m2}")
print(f"Subject 3: {m3}")
print(f"Total: {total}")
print(f"Average: {average:.2f}")
Sample Output
Enter student name: Rahul
Enter marks of three subjects: 80 75 90
----- STUDENT RESULT -----
Name: Rahul
Subject 1: 80
Subject 2: 75
Subject 3: 90
Total: 245
Average: 81.67
28. Difference Between input() and print()
| input() | print() |
|---|---|
| Accepts data from the user | Displays data to the user |
| Returns entered data as a string | Displays values on the screen |
| Used for input | Used for output |
input("Enter name: ") |
print("Hello") |
29. Important Points for Students
print()is used for displaying output.input()is used for accepting user input.input()returns data as a string.- Use
int()for integer input. - Use
float()for decimal input. - Use
split()to separate multiple input values. - Use
map()to convert multiple input values efficiently. - Use
septo control the separator between printed values. - Use
endto control what is printed at the end of aprint()call. - Use f-strings for readable formatted output.
30. Quick Reference
| Statement / Function | Use |
|---|---|
input() |
Take input from user |
print() |
Display output |
int() |
Convert to integer |
float() |
Convert to float |
str() |
Convert to string |
split() |
Separate input values |
map() |
Apply conversion to multiple values |
sep |
Set separator in output |
end |
Control ending of output |
31. Summary
Input and Output statements are essential for creating interactive Python programs.
The input() function allows a program to receive information from the user,
while print() displays information to the user.
By combining input, type conversion, processing and formatted output, Python programmers can create useful applications such as calculators, student-result systems, billing programs and data-entry applications.
Created by Bijan Krishna Paul
Computer Science • Python Programming • Data Science
No comments:
Post a Comment