COGNITION FOUNDRYLEARN. BUILD. UNDERSTAND.
PY-022 / A safe input boundary◷ 21 min read + practice

Accepting and validating input

When a program asks someone to paste a path, the answer is just text. Before doing anything with that text, decide which inputs you accept and what you do when they do not meet the rules.

By the end, you can: Explain what input() returns, validate text and practise input-processing functions with browser-friendly examples.
Before this lesson: PY-017 · PY-019 · PY-021

input() belongs to an interactive terminal

In a locally run Python program, input("Enter a value: ") displays a prompt and reads one line entered by the user. It returns a string, even if the user typed only digits.

This browser playground is not an interactive terminal. The editable examples use sample strings so Run can finish immediately. On a computer with Python installed, the same functions can process text read using input().

Validation is a decision, not just an error

The function str.strip() can help detect a blank response. A validation function might reject empty input but leave internal spaces untouched, since a path like C:\My Files\readme.txt can be perfectly valid.

This browser playground is not an interactive terminal: examples use sample strings so Run can complete immediately. On your own computer, input() reads a line typed into the terminal. The same validation functions can be used with either kind of string.

Keep conversion errors visible

Keep validation separate from presenting the result. A function accepts a string and either returns a useful value or raises an error that its caller can explain.

A function that accepts a string can be tested without asking a person to type repeatedly. Use fixed examples, including empty and unusual inputs, to check the rules before using real input.

Separate input from work

A small function accepts a string and returns a result. That function should be testable with predefined values without needing anyone to type a response each time.

Keep text processing separate from the way the value is obtained. The same function can work with a sample string today and with a string returned by input() in a local program.

Try it yourself.

Choose a starting example or write something completely different. You control the code; the Run button executes what is in the editor, not a hidden preset.

CF Python PlaygroundBrowser-based Python
Ready to load Python
Validate a blank input

Reject text that is empty after trimming surrounding whitespace.

Syntax colours highlight Python code. Tab inserts four spaces; press Escape then Tab to leave the editor. Ctrl+Enter runs the current code.
OUTPUT / ERRORS
Press Run Python to execute the editable code.
NOW MAKE IT YOURS

Try a string containing only spaces and examine the error.

Need a hint?

Change the argument passed to require_text() to " ".

On first Run, a pinned Python runtime downloads from jsDelivr. Code executes locally in your browser's Web Worker, not on our server. It cannot inspect your normal PC files or Windows hardware. Avoid entering secrets. Heavy/infinite loops are stopped by a time limit.

Check your understanding.

Try answering each question before opening its reference answer. Explain the idea aloud or change an example to test it for yourself.

  1. Why do we test our transformation function with fixed strings in the browser instead of calling input() directly?

    View answer

    The browser playground runs Python in an isolated worker rather than an interactive terminal with a normal input stream. Using a sample string makes the demonstration predictable and still allows the validation function to execute. On a computer running Python in a terminal, input() obtains a string from the user that you can pass into the same function.

  2. Why should validation avoid rewriting interior spaces in an otherwise non-empty path?

    View answer

    An interior space can be part of a legitimate folder or filename. Validation should decide whether the input is acceptable, such as rejecting an empty response, without silently changing characters that identify a real file. Cleaning and validation may cooperate, but each operation should have a clear and explicit purpose.

Where this leads.

Now we can represent paths more cleanly using the Python standard library.