Python Input

In Python, the input() function is used to accept user input from the keyboard. It reads a line of text entered by the user and returns it as a string. You can use this input in your Python programs for a wide range of applications, such as collecting user data, creating interactive text-based applications, or customizing program behavior based on user input.

Here's a detailed explanation of Python's input() function, along with examples:

Syntax:

        
variable_name = input("Prompt: ")
        
    

- variable_name: This is the name of the variable where the user's input will be stored. The input is always returned as a string, but you can convert it to other data types as needed.

- "Prompt:" (optional): This is an optional string that is displayed to the user as a prompt. It's useful for providing guidance to the user about what kind of input is expected.

Example 1: Basic Input

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

In this example, the input() function displays the prompt "Enter your name: " and waits for the user to enter their name. The entered text is stored in the variable name, and it is then displayed as part of a greeting message.

Example 2: Numeric Input and Type Conversion

        
age = input("Enter your age: ")
age = int(age)  # Convert the input to an integer
if age >= 18:
    print("You are an adult.")
else:
    print("You are a minor.")
        
    

Here, the input() function collects the user's age as input, but it's initially stored as a string. To perform numeric comparisons, the string is converted to an integer using int(). The program then checks if the user is an adult based on the age input.

Example 3: Evaluating Mathematical Expressions

        
expression = input("Enter a mathematical expression: ")
result = eval(expression)
print("Result:", result)
        
    

In this example, the input() function collects a mathematical expression as input, and the eval() function is used to evaluate the expression. Be cautious when using eval() with user input, as it can execute arbitrary code.

Example 4: Handling Multiple Inputs

        
data = input("Enter name, age, and city (comma-separated): ")
name, age, city = data.split(',')
print("Name:", name)
print("Age:", age)
print("City:", city)
        
    

Here, the user is asked to input name, age, and city, separated by commas. The input is split into individual variables using the split() method, and each value is displayed separately.

Example 5: Error Handling

        
while True:
    try:
        number = int(input("Enter an integer: "))
        print("You entered:", number)
        break
    except ValueError:
        print("Invalid input. Please enter an integer.")
    

        
    

This example demonstrates error handling. It repeatedly asks the user for input until a valid integer is entered. If the input cannot be converted to an integer, a ValueError exception is caught, and an error message is displayed.

The input() function is a versatile tool for collecting user input in Python. Just remember to handle user input carefully to ensure your program is robust and secure.