COGNITION FOUNDRYLEARN. BUILD. UNDERSTAND.
PY-005 / Working with text◷ 19 min read + practice

Strings and text

Text arrives everywhere: filenames, names, messages and user input. Python represents it with strings. Being precise about the difference between text and source-code syntax prevents subtle bugs.

By the end, you can: Build strings with variables, use useful methods and recognise the difference between a string value and its Python source spelling.
Before this lesson: PY-004

Quoting text

Single quotes and double quotes both delimit Python string literals. For example, "hello" and 'hello' represent the same value. Choose whichever makes the text easiest to read.

Backslashes can introduce escape sequences in source code: \n represents a newline and \t a tab. This matters when teaching Windows paths. A raw string literal such as r"C:\Temp" changes how most backslashes are interpreted when Python reads source code.

Create messages with f-strings

An f-string puts a computed value directly into readable text. Place f before the opening quote and wrap the expression in braces. For example, f"Hello, {name}" includes the current value of name.

f-strings are useful when reporting totals, generating status messages and eventually formatting dates.

Useful string operations

len(text) counts characters. text.strip() removes surrounding whitespace, while text.strip("\"") can remove surrounding double-quote characters. strip() does not mean “remove only one matching pair”; it removes matching characters from the ends, so you should choose the operation intentionally.

The source code spelling of a string and its stored value are different ideas. Escaping backslashes in Python source should not be confused with rewriting the real filesystem path.

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
Readable f-strings

Build a message using previously named values.

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

Create a variable for a different name and change the output.

Need a hint?

Edit the assignment on the first line.

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. What is an f-string and what does its f prefix do?

    View answer

    An f-string starts with an f before its quotation marks, for example f"Hi, {name}". Expressions in curly braces are evaluated and placed into the resulting string. The f prefix is not printed; it changes how Python interprets the literal.

  2. Does adding two backslashes to Python source necessarily change the actual file path?

    View answer

    Not necessarily. In a normal Python string literal, "C:\\Temp" spells a value containing one backslash before Temp. A raw literal, such as r"C:\Temp", can express that same value differently. The source spelling and the runtime string must be distinguished; raw strings also have limitations, such as not ending in a single backslash.

Where this leads.

Text lets us communicate results; the next lesson makes the calculations behind those results more reliable.