Python User Input
Python User Input
In Python, you can get user input using the built-in input() function. This allows you to interact with users and take their responses during the execution of your program.
Syntax of input()
input(prompt)
prompt(optional): A string that is displayed to the user before the input is taken. This can be left empty, but it is common to provide a prompt message.- The
input()function returns the input as a string, regardless of the data type entered by the user.
Basic Example
# Getting user input
name = input("Enter your name: ")
# Output the result
print("Hello, " + name + "!")
Output Example:
Enter your name: Alice
Hello, Alice!
Converting Input to Other Data Types
Since the input() function always returns a string, you may want to convert it to other data types such as integers or floating-point numbers. You can do this using type casting.
Example 1: Converting to Integer
age = int(input("Enter your age: "))
print("You are", age, "years old.")
Output Example:
Enter your age: 25
You are 25 years old.
Example 2: Converting to Float
height = float(input("Enter your height in meters: "))
print("Your height is", height, "meters.")
Output Example:
Enter your height in meters: 1.75
Your height is 1.75 meters.
Handling Invalid Input
If the user enters an input that cannot be converted to the desired type, a ValueError will be raised. You can handle this using a try...except block to provide a better user experience.
Example 3: Handling Invalid Input with Try…Except
while True:
try:
number = int(input("Enter a number: "))
break # Exit the loop if valid input is entered
except ValueError:
print("That's not a valid number. Please try again.")
Output Example:
Enter a number: abc
That's not a valid number. Please try again.
Enter a number: 10
Getting Multiple Inputs
You can use the input() function multiple times to get different pieces of information from the user, or you can split a single line of input into multiple values.
Example 4: Getting Multiple Inputs in One Line
You can ask the user to enter values separated by spaces and use the split() method to break the input into a list.
name, age = input("Enter your name and age separated by space: ").split()
age = int(age)
print(f"Hello {name}, you are {age} years old.")
Output Example:
Enter your name and age separated by space: Alice 25
Hello Alice, you are 25 years old.
Example 5: Getting Multiple Inputs of Different Types
name, age, height = input("Enter your name, age, and height separated by space: ").split()
age = int(age)
height = float(height)
print(f"Hello {name}, you are {age} years old and {height} meters tall.")
Output Example:
Enter your name, age, and height separated by space: Bob 30 1.75
Hello Bob, you are 30 years old and 1.75 meters tall.
Getting a Yes/No Response
You can use the input() function to prompt the user for a simple “yes” or “no” response and handle it appropriately.
Example 6: Yes/No Input
response = input("Do you like Python? (yes/no): ").lower()
if response == "yes":
print("That's great!")
elif response == "no":
print("That's unfortunate.")
else:
print("Please answer with 'yes' or 'no'.")
Output Example:
Do you like Python? (yes/no): yes
That's great!
Prompting for Password Input
If you want to ask the user for a password without showing the input on the screen, you can use getpass.getpass(), which hides the input.
Example 7: Password Input
import getpass
password = getpass.getpass("Enter your password: ")
print("Password entered.")
Output Example:
Enter your password: ********
Password entered.
Summary
input(): Reads input from the user as a string.- Type conversion: You can convert the string input into other data types like
intorfloat. - Error handling: Use
try...exceptto manage invalid inputs. - Multiple inputs: Use
split()to get multiple values in one line. - Password input: Use
getpass.getpass()to securely handle passwords.