COGNITION FOUNDRYLEARN. BUILD. UNDERSTAND.
PY-024 / Apply what you know◷ 20 min read + practice

Practice with string functions

You have learned individual string operations and reusable functions. Now practise combining them in short examples, changing the inputs and checking what each function returns. Every example runs in the browser editor.

By the end, you can: Combine familiar string methods, decisions and functions; verify predictable results with simple checks.
Before this lesson: PY-011 · PY-013 · PY-015 · PY-017 · PY-019 · PY-020

Choose transformations carefully

Cleaning text depends on what it represents. Removing surrounding whitespace from a label is often harmless; removing spaces from a filename or password could change its meaning.

Use a small function with one clear purpose, such as turning repeated whitespace in a display label into single spaces. Test it with more than one input before reusing it.

Replace only what you intended

The string method replace(old, new, count) creates a new string. A count of 1 changes only the first match, which can be important when a marker appears more than once.

Keep the original unchanged so you can compare it with the result. That is a useful habit when experimenting with text operations.

Handle blank input explicitly

strip() returns an empty string when a value contains only whitespace. Use if not text: when a blank value needs a clear response rather than continuing with unsuitable data.

Separating validation from formatting helps each part stay understandable. A simple if branch can be enough; not every small function needs elaborate error handling.

Check your assumptions with tests

assert compares a result with the behaviour you expect and raises an error if the condition is false. It is useful for learning checks; programs intended for users should not rely on assertions for validating untrusted input.

Test a normal case and a case where no transformation is needed. Try deliberately changing one expected result and read the error to understand why the check failed.

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
Tidy a display label

Combine strip(), split() and join() for labels.

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 label containing tabs or line breaks and compare the output.

Need a hint?

Change the text between quotation marks, leaving the function unchanged.

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 is whitespace normalisation suitable for a display label but risky for a filename?

    View answer

    A display label often treats extra spaces or line breaks as accidental formatting, so collapsing whitespace can make it easier to read. A filename is an exact identifier: spaces, dots and other characters can be significant. A cleaning function should be chosen according to what the text means, not applied automatically everywhere.

  2. What two different situations do the remove_suffix() assertions check?

    View answer

    The first assertion checks that a matching .txt suffix is removed. The second checks that a non-matching .txt suffix leaves report.csv unchanged. Testing both confirms the function changes only its intended inputs and does not damage other strings.

Where this leads.

You have practised combining small Python operations into predictable functions. Keep experimenting with new inputs, and move on to further topics as the course grows.