Course Content
Python Indentation, Comments and Variables
0/2
Object Oriented Programming in Python
0/1
Exception Handling in Python
0/1
Sending emails with Python
0/1
Unit test in python programming
0/1
Python programming (zero to advance!!!)
About Lesson
Inputs from users

In Python, the `input()` function is used to accept user input from the keyboard. Here’s how it works:

### Syntax:
“`python
input(prompt)
“`

– `prompt`: This is the message or prompt displayed to the user, indicating what input is expected.

### Example:
“`python
name = input(“Enter your name: “)
print(“Hello, ” + name + “!”)
“`

In this example:
– The `input(“Enter your name: “)` statement prompts the user to enter their name.
– Whatever the user types in response to the prompt is stored in the variable `name`.
– The program then prints a greeting message using the provided name.

### Handling User Input
The `input()` function always returns a string, regardless of what the user enters. If you expect a different data type, such as an integer or a floating-point number, you need to convert the input using appropriate conversion functions like `int()` or `float()`.

### Example:
“`python
age = int(input(“Enter your age: “))
print(“You will be”, age + 10, “years old in 10 years.”)
“`

In this example:
– The `input()` function retrieves the user’s input as a string.
– The `int()` function converts the input string to an integer before storing it in the variable `age`.
– The program then performs arithmetic with the integer value obtained from the user input.

### Note:
– Be cautious when using `input()` with user-provided data, as it can raise security concerns such as injection attacks if not properly validated.
– Ensure that the prompt provided to the user is clear and informative, guiding them on what input is expected.

Understanding how to use the `input()` function enables Python programs to interact with users dynamically, enhancing their functionality and usability.
Join the conversation