In python, the input()
function is used to take input from the user.
Syntax for input()
function:
variable = input(prompt)
prompt
: A string that is displayed to the user before taking input. If not provided, the function will simply wait for user input without displaying any prompt.variable
: The variable where the user's input is stored.
For example:
name = input("Enter your name: ")
print("Hello "+ name)
# Example output:
# Enter your name: John
# Hello John
- The
input()
function prompts the user with "Enter your name: ". - The user types something, and that input is stored as a string in the
name
variable. - The
print()
function then outputs the message "Hello [name]" to the console.
Note: input()
always returns a string:
Even if the user enters a number or other types of data, it will be treated as a string. If you need the input to be a different type (like an integer or float), you'll need to convert it explicitly using functions like int()
or float()
.
For example:
age = input("Enter your age: ") # Input is stored as string by default
age = int(age) # Convert string to integer
print("Your age is:", age)
# Example output:
# Enter your age: 25
# Your age is: 25