Python User Input
Python · Tooling & Errors
Theory
input(prompt) pauses the program, shows the prompt text, waits for the user to type a line and press Enter, and returns what they typed as a string - always a string, even if they typed 25. To use it as a number, convert it: age = int(input('Age: ')) or price = float(input('Price: ')).
Conversion fails if the text is not a valid number: int('abc') raises ValueError, so real scripts wrap it in try/except and ask again. .strip() removes accidental spaces, and .lower() helps compare answers like 'Yes'/'yes'. input() needs an interactive terminal; in notebooks it shows a text box, and in automated jobs or browser sandboxes there is no user, which is why the course runner simulates it with a fixed value.
Why we use it
input() is the simplest way to make a script interactive - asking for a file name, a choice or a number - without building a web page or GUI.
When to use it
Use it for small command-line tools, practice programs and quick prompts. For scripts that run automatically, read values from command-line arguments (sys.argv, argparse), config files or environment variables instead.
How it works
- input('Enter your age: ')
- Show the prompt and wait for Enter
- Return the typed text as a string
- Is a number needed? Yes: Convert with int() or float(). No: Use the string as it is.
- Did conversion raise ValueError? Yes: Show an error and ask again. No: Continue with the value.
- Use the validated input
Syntax
input(prompt)Example
name = input('Enter your name: ')Key points
- Cast with int()/float() to read numbers
- Commonly used in simple CLI scripts
- input() always returns a str
- Wrap conversions in try/except ValueError
- Not suitable for automated or scheduled jobs - use arguments or config