input function in Python
What is input() in Python? The input() function is used to take input from the user during program execution. It always returns the user input as a string.
Basic Syntax input(prompt)
The function has the following parameter:
• prompt (Optional) A message that appears on the screen to tell the user what to enter.
Return Value: • Always returns a string (str).
Example: Simple Input.
Python
# Taking user input
name = input("Enter your name: ")
# Displaying the input
print("Hello,", name) Output: Enter your name: Ashish
Hello, Ashish
Explanation:
- The text inside input() ("Enter your name: ") is shown to the user.
- The user types something (e.g., Ashish).
- That value is stored in the variable name as a string.
Example: Input as a Number. By default, input() returns a string, so if we want a numeric value, you must convert it using int() or float() functions.
Python
# Taking numeric input
age = int(input("Enter your age: "))
# Using the numeric value
print("Next year you will be:", age + 1) Output: Enter your age: 25
Next year you will be: 26
Explanation:
- input() gives "25" (a string).
- int() converts it to an integer → 25.
- Then arithmetic operations like + 1 work correctly.
Example: Input Without Prompt If we don’t give any message inside input(), it just waits for user input.
Python
data = input()
print("You entered:", data)