Python User Input


In Python, you can take input from users using the built-in input() function. It lets your program interact with users during runtime.


🔹 Getting User Input

Use input() to ask the user to type something:

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

✅ The input from the user is always returned as a string.


🔹 Converting Input to Numbers

Since input() returns a string, you’ll often need to convert it to int or float:

age = input("Enter your age: ")
age = int(age)
print("You will be", age + 1, "next year.")

🔹 Float Input Example

price = float(input("Enter the price: "))
print("Total price after tax:", price * 1.18)

🔹 Input with Multiple Variables

x = int(input("Enter first number: "))
y = int(input("Enter second number: "))
print("Sum is:", x + y)

🔹 Type Checking (Optional)

Always validate the input if needed:

data = input("Enter a number: ")
if data.isdigit():
    print("It's a number!")
else:
    print("Invalid input")

🔹 Multiline Input (Advanced)

For multiple lines, you can loop:

print("Enter lines (type 'end' to finish):")
lines = []
while True:
    line = input()
    if line.lower() == "end":
        break
    lines.append(line)

Practice Questions

Q1. Write a Python program to ask the user to enter their first and last name, then print the full name.

Q2. Write a Python program to take input of two numbers and print their product.

Q3. Write a Python program to ask for temperature in Celsius and convert it to Fahrenheit.

Q4. Write a Python program to ask the user for their favorite color and display it in a sentence.

Q5. Write a Python program to input a number and check if it is even or odd.

Q6. Write a Python program to ask the user to enter their age, convert it to an integer, and check if they are 18 or older.

Q7. Write a Python program to take 3 float inputs and print their average.

Q8. Write a Python program to input a name and print its length using len().

Q9. Write a Python program to read two numbers from the user and print the larger one.

Q10. Write a Python program to input a string and print it in uppercase.


Go Back Top