English
Summary
Conclusions
A str is an immutable sequence of code points, while bytes represents encoded bytes. String methods are sufficient for simple cleaning and splitting; Unicode normalization is needed to compare canonically equivalent forms. F-strings produce text immediately, while t-strings preserve static parts and values for a custom processor. A regular expression checks structure, but semantic constraints on dates, amounts, and other data are checked separately.
Self-check questions
- What exactly does
lencount forstrand forbytes? - Why does
replacenot change the original string? - How do
split()andsplit(" ")differ? - Why is
strip(".txt")not a way to remove an extension? - Why is NFC normalization needed?
- How do the purposes of
casefoldandlowerdiffer? - What do width, precision, and type mean in a format specification?
- When is
!ruseful, and when is it incompatible with a numeric format? - What type does a t-string create, and when are its expressions evaluated?
- Why does the
tprefix alone not guarantee HTML safety? - How do the two classes named
Templatediffer? - When do you use
search,match, andfullmatch? - How do capturing groups affect the
findallresult? - Why pass a function to
re.sub? - Why does matching a regex not yet prove that a date is valid?
Useful links
- https://docs.python.org/3.14/library/stdtypes.html#text-sequence-type-str.
- https://docs.python.org/3.14/howto/unicode.html.
- https://docs.python.org/3.14/library/string.html.
- https://docs.python.org/3.14/library/string.templatelib.html.
- https://peps.python.org/pep-0750/.
- https://docs.python.org/3.14/howto/regex.html.