← Back to Python Course | Chapter 2: Input, Output & Operators | Lesson 2 of 16

Python input()

Basic input() Usage

input() pauses your program and waits for the user to type something and press Enter; whatever they typed is returned as a string, regardless of whether it looked like a number. This is Python's standard way to make a script interactive from the command line.

Example: Basic input() Usage

python
# name = input("Enter your name: ")
name = "Alex"  # simulated input for this sandbox
print("Hello,", name)

Converting Inputs to Integers

Since input() always hands back text, wrapping the call in int() — like age = int(input("Age: ")) — converts that text into a whole number so you can safely use it in arithmetic or numeric comparisons.

Example: Converting Inputs to Integers

python
# age = int(input("Age: "))
age = int("25")
print(age + 1)

Converting Inputs to Floats

For values with a decimal point, float() performs the same job as int() but produces a floating-point number instead, which matters for calculations like averages or measurements where a whole number would lose precision.

Example: Converting Inputs to Floats

python
# price = float(input("Price: "))
price = float("19.99")
print(price * 2)

Reading Multiple Inputs

Calling .split() on the string returned by input() breaks it into a list of separate pieces wherever whitespace occurs, which is the standard way to read several values a user typed on a single line, like x, y = input().split().

Example: Reading Multiple Inputs

python
# x, y = input().split()
x, y = "3 4".split()
print(x, y)

Handling Input Types

If a user types letters where int() or float() expects a number, Python raises a ValueError and the program crashes unless you catch it — validating or wrapping conversions in a try/except block is essential for any program that trusts real user input.

Example: Handling Input Types

python
text = "abc"
try:
    number = int(text)
except ValueError:
    print("That's not a valid number")

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.