iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Python User Input

input() reads a line from standard input. It always returns a string — cast it if you need a number.

Basic

PYTHON
name = input('Your name: ')
print(f'Hello, {name}!')

Cast to a number

PYTHON
age = int(input('Age: '))
print(f'In a decade you\\'ll be {age + 10}')

Validate & retry

PYTHON
while True:
    raw = input('Age: ').strip()
    try:
        age = int(raw)
    except ValueError:
        print('Please enter a whole number.')
        continue
    if age < 0:
        print('No time-travellers, please.')
        continue
    break
print('Got', age)

Multi-value input

PYTHON
nums = input('Two numbers: ').split()
a, b = int(nums[0]), int(nums[1])
print(a + b)

Reading until EOF

PYTHON
import sys
lines = sys.stdin.read().splitlines()
print(len(lines), 'lines read')

In the browser editor

The iwantcoding.com editor uses Pyodide, which can't pause for terminal input. Substitute a fixed value in examples that would otherwise prompt:

PYTHON
# name = input('Your name: ')   # would block in browser
name = 'Ada'
print(f'Hello, {name}!')
Tip: For richer CLIs reach for argparse (standard library) or click/typer (PyPI). They handle flags, help text, types, defaults, and subcommands.

Example

Example
# input() blocks in the Pyodide editor — substitute a fixed value to demo:
name = 'Ada'  # name = input('Your name: ')
print(f'Hello, {name}!')
Try it Yourself »

Exercise

Read a line from standard input.

name = ('Your name: ')

Test yourself

Q1. input() returns…
Q2. In the iwantcoding.com editor, input()…
Q3. For complex CLIs use…

Discussion

Loading…