Python User Input

In Python, you can take input from the user using the built-in input() function. This allows programs to be dynamic and interactive.

Basic Syntax

input("Enter something: ")
  • It pauses program execution and waits for user input.
  • Always returns string type.

Example 1: Getting a Simple String Input

name = input("What is your name? ")
print("Hello,", name)

Output:

What is your name? John 

 Hello, John

Example 2: Taking Numeric Input

Since input() returns a string, you need to convert it:

age = int(input("Enter your age: "))
print("You are", age, "years old.")

Output:

Enter your age: 25  
You are 25 years old.

Example 3: Taking Float Input

salary = float(input("Enter your salary: "))
print("Monthly Salary:", salary)

Multiple Inputs in One Line

You can take space-separated values using split():

x, y = input("Enter two numbers: ").split()
print("x =", x)
print("y =", y)

Output:

Enter two numbers: 5 10  
x = 5  
y = 10

Example: Add Two Numbers

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Sum:", a + b)

Tips for User Input

TipExample
Convert input to numberint(input()), float(input())
Handle multiple inputsinput().split()
Strip extra whitespaceinput().strip()
Use try-except for safetyCatch invalid inputs