Python Input and Output
Input and output are basic operations in Python. Input is used to accept data from the user, while output is used to display information on the screen.
1. Python Output Using print()
The print() function is used to display text, values,
variables and expressions on the screen.
print("Hello Python")
print(100)
print(10 + 20)
Output:
Hello Python
100
30
2. Printing Variables
name = "Aadarsh"
age = 42
print(name)
print(age)
Output:
Aadarsh
42
3. Printing Multiple Values
Multiple values can be passed to print() by separating
them with commas.
name = "Aadarsh"
age = 42
print("Name:", name)
print("Age:", age)
Output:
Name: Aadarsh
Age: 42
4. The sep Parameter
The sep parameter specifies the separator between
multiple values.
print("Python", "Java", "C++", sep=" | ")
Output:
Python | Java | C++
5. The end Parameter
By default, print() moves to a new line after displaying
the output. The end parameter can change this behaviour.
print("Hello", end=" ")
print("Python")
Output:
Hello Python
6. Formatted Output Using f-Strings
f-strings provide a simple way to insert variables inside a string.
name = "Aadarsh"
marks = 85
print(f"Name: {name}")
print(f"Marks: {marks}")
Output:
Name: Aadarsh
Marks: 85
7. Taking Input Using input()
The input() function is used to accept data from the user.
The value returned by input() is always a string.
name = input("Enter your name: ")
print("Hello", name)
Example:
Enter your name: Rahul
Hello Rahul
8. Taking Integer Input
Since input() returns a string, use int()
when an integer is required.
age = int(input("Enter your age: "))
print("Age:", age)
Example:
Enter your age: 20
Age: 20
9. Taking Floating-Point Input
Use float() to convert the input into a floating-point number.
price = float(input("Enter price: "))
print("Price:", price)
Example:
Enter price: 99.50
Price: 99.5
10. Taking Multiple Inputs
Multiple values can be accepted using split().
a, b = input("Enter two numbers: ").split()
print("First:", a)
print("Second:", b)
Example:
Enter two numbers: 10 20
First: 10
Second: 20
11. Taking Multiple Integer Inputs
The map() function can be used with int()
to convert multiple inputs into integers.
a, b = map(int, input("Enter two numbers: ").split())
print("Sum =", a + b)
Example:
Enter two numbers: 10 20
Sum = 30
12. Taking Multiple Floating-Point Inputs
x, y = map(float, input("Enter two values: ").split())
print("Total =", x + y)
Example:
Enter two values: 10.5 20.5
Total = 31.0
13. Input as a List
Multiple values can be converted into a list using split().
numbers = input("Enter numbers: ").split()
print(numbers)
Example:
Enter numbers: 10 20 30 40
['10', '20', '30', '40']
To create a list of integers:
numbers = list(map(int, input("Enter numbers: ").split()))
print(numbers)
Output:
Enter numbers: 10 20 30 40
[10, 20, 30, 40]
14. Escape Characters in Output
Escape characters are used to control the formatting of output.
| Escape Character | Meaning |
|---|---|
\n |
New line |
\t |
Tab |
\\ |
Backslash |
\" |
Double quotation mark |
\' |
Single quotation mark |
print("Hello\nPython")
print("Name\tAge")
Output:
Hello
Python
Name Age
15. Using format() for Output
The format() method can also be used to insert values
into strings.
name = "Rahul"
marks = 90
print("Name: {}, Marks: {}".format(name, marks))
Output:
Name: Rahul, Marks: 90
16. Input and Output Example
The following program accepts two numbers and displays their sum.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
sum_value = a + b
print("Sum =", sum_value)
Example:
Enter first number: 15
Enter second number: 25
Sum = 40
17. Important Points
input()accepts data from the user.input()returns a string by default.- Use
int()for integer input. - Use
float()for decimal input. print()displays output on the screen.sepcontrols the separator between printed values.endcontrols what is printed after the output.split()is useful for taking multiple inputs.map()can be used to convert multiple input values.
input() always returns a
string. Therefore, type conversion such as int() or
float() is required when numerical calculations are performed.