User Input
-
User input allows Python programs to interact with the user. The input() function is used to accept data typed by the user, such as names, numbers, or any custom information. By default, input is always taken as a string, so you can convert it into integers or floating-point numbers using int() or float() when needed.
What Is User Input?
User input allows your program to interact with the person using it.
When Python reaches an input() statement, it pauses and waits for the user to type something.
After the user presses Enter, the program continues using the entered value.๐ฆ Using a Prompt Inside input()
Instead of writing a message in print(), you can put the message directly inside input().
Getting User Input with a Prompt
This example shows how to display a message directly inside the input() function. The user is asked to enter their full name, and Python then uses that input to display a personalized greeting.
fullname = input("Enter your full name: ")
print(f"Hello, {fullname}! Nice to meet you.")
๐ Taking Input and Converting It Immediately
When you read a value using input(), Python always stores it as a string, even if the user types a number.
To use the value in calculations, you must convert it to the correct data type (like int or float) right away.
Reading Age Input and Converting to Integer
This example shows how to take user input using input(), convert the entered value into an integer using int(), and then display it in a message. This is useful when you need numerical input for calculations or age-based logic.
age = int(input("Enter your age: "))
print("You are", age, "years old.")
๐ Taking Multiple Inputs
You can gather multiple details from the user by calling the input() function several times.
Each input() statement pauses the program until the user types a response, allowing you to create interactive forms, quizzes, or user-driven applications.
User Registration Form
This program collects three different details from the userโemail, age, and favorite programming language. It then prints a confirmation message summarizing the information.
email = input("Enter your email: ")
age = input("Enter your age: ")
language = input("Your favorite programming language: ")
print(f"\nRegistration Successful!")
print(f"Email: {email}")
print(f"Age: {age}")
print(f"Favorite Language: {language}")