By the end of this lesson
Extract a substring and create a readable message with an f-string.
Before you start
- Python 3.12 or a compatible newer Python 3 interpreter; no external packages.
- Complete lesson 3 in this route, or be comfortable with its examples.
Three words to know
- String
- A sequence of text characters, represented by str.
- Slice
- A selected range with the end position excluded.
- Method
- An operation called on a value using a dot.
1. Text has positions
Positions start at zero. A slice such as name[:4] selects the first four characters. Strings are immutable: methods return new strings instead of editing individual characters in place. A character count is not necessarily a byte count in a UTF-8 file.
name = "report.json"
print(name[0])
print(name[:6])
print(name.endswith(".json"))
What you should see · example output
r report True
2. Normalise only when appropriate
strip removes surrounding whitespace. lower changes letter case. These are convenient for a practice choice but may damage meaningful whitespace or case-sensitive identifiers. Preserve the original input when its exact spelling matters.
raw = " PYTHON "
clean = raw.strip().lower()
print(clean)
print(raw == clean)
What you should see · example output
python False
3. Format for a reader
An f-string inserts expressions between braces. The formatting below pads a number to two digits; it changes the display, not the underlying value. A newline escape inside a string creates a line break when printed.
step = 3
language = "Python"
print(f"Lesson {step:02d}: {language}")
print("Read\nPractice")
What you should see · example output
Lesson 03: Python Read Practice
YOUR TURN
A small check before you continue
For name = "sample.csv", predict name[-3:] and name.endswith(".json").
Show a suggested answer
The slice is csv and the suffix check is False. This only inspects text; it does not check whether the file exists or whether its contents are valid CSV.
Sources and scope
Documentation and examples reviewed 2026-10-18. Provider screens, package versions and output wording can differ. Examples show expected outcomes; they are not a claim that OpenViST connected to your server or verified your setup.
