7. Strings and formatting¶
Strings are str, and you’ll often build output using f-strings.
String methods¶
def main() -> None:
raw = " Alice "
cleaned = raw.strip().lower()
println(cleaned)
F-strings (interpolation)¶
def main() -> None:
name = "Alice"
age = 30
println(f"{name} age={age}")
Try it¶
- Normalize an input string with
strip().lower(). - Build an output line using an f-string.
- Use one string method you didn’t use yet (for example
upper()).
One possible solution
def main() -> None:
raw = " Alice "
cleaned = raw.strip().lower()
println(f"cleaned={cleaned}")
println(cleaned.upper())
Where to learn more¶
- Full strings guide: Strings
Next¶
Back: 6. Errors (Result/Option and ?)
Next chapter: 8. Collections and iteration