Lessons
Courses
Notes in current language not found. Defaulting to English.

Input Function in Python [ English ]

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

  • prompt (optional)A message displayed to the user indicating what input is expected.

3. Return Value

  • Returns the user input as a string (str).

4. Basic Example

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

Sample Output

Enter your name: RahulHello Rahul

Explanation

  • The program asks the user to enter a name.
  • The entered value is stored in the variable name.
  • It is then displayed using print().

5. Input Always Returns String

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

Output

<class 'str'>

Explanation

  • Even if the user enters a number, it is treated as a string.

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

  • input() always returns string data
  • Use type conversion (int(), float()) when needed
  • Prompt message helps users understand what to enter
  • Useful for interactive programs

10. Summary

  • input() is used to take user input from the keyboard.
  • It returns data as a string by default.
  • Type conversion is required for numeric operations.
  • It is essential for creating interactive Python programs.