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.


Python User Input Quiz

Q1: What is the default data type returned by input()?

A. int
B. str
C. float
D. bool

Q2: Which function is used to take input from a user?

A. read()
B. input()
C. get()
D. type()

Q3: How do you convert user input into an integer?

A. str()
B. int()
C. float()
D. input()

Q4: What does int(input()) do?

A. Takes string input
B. Converts input to string
C. Takes and converts input to integer
D. Takes float input

Q5: What will happen if a non-number is converted using int()?

A. 0 is returned
B. False is returned
C. Error is raised
D. Nothing happens

Q6: Which input is best for decimal values?

A. int()
B. input()
C. float(input())
D. str()

Q7: Which method can check if input is a number?

A. input()
B. isnumeric()
C. isdigit()
D. int()

Q8: What does this code do? input("Enter value: ")

A. Assigns a value to variable
B. Prints value
C. Asks for input and returns string
D. Calls a function

Q9: Which of these is True about input()?

A. It can take multiple variables at once
B. It always returns an integer
C. It returns user input as string
D. It returns None

Q10: What should you do before using user input in calculations?

A. Print it
B. Escape it
C. Convert to int or float
D. Clear it

Go Back Top