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.
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.
Build a message using previously named values.
Press Run Python to execute the editable code.
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.
What is an f-string and what does its f prefix do?
View answer
An f-string starts with an
fbefore its quotation marks, for examplef"Hi, {name}". Expressions in curly braces are evaluated and placed into the resulting string. Thefprefix is not printed; it changes how Python interprets the literal.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 beforeTemp. A raw literal, such asr"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.