Notes

Input Function in Python [ English ]

< Prev Next >

1. Introduction

The input() function in Python is used to take input from the user during program execution. It allows interaction between the user and the program by accepting data entered through the keyboard.

The data entered by the user is always returned as a string by default.

2. Syntax

input(prompt)

Parameter

3. Return Value

4. Basic Example

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

Sample Output

Enter your name: Rahul
Hello Rahul

Explanation

5. Input Always Returns String

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

Output

<class 'str'>

Explanation

6. Converting Input to Other Data Types

To use numeric values, we need to convert the input.

Example: Integer Input

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

Example: Float Input

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

7. Taking Multiple Inputs

Method 1: Using Separate Inputs

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

Method 2: Using split()

a, b = input("Enter two numbers: ").split()
print(int(a) + int(b))

8. Example: Simple Calculator

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

sum = num1 + num2

print("Sum =", sum)

9. Important Points

10. Summary

< Prev Next >