Programming with Python › Python Setup and First Programs › Day 45

Day 45: Strings and Text Processing

Day 45 of 365 — Strings and Text Processing

After this lesson you will handle text in Python with confidence — creating, slicing, cleaning, and formatting strings — and understand why strings are immutable Unicode sequences and how they become the bytes that travel to files and services.

Course
Programming with Python
Category
Python Setup and First Programs
Reading time
≈ 40 min
Practical time
≈ 30 min
Lesson duration
1h 10m
Last verified
2026-07-12

Hands-on lab for this lesson

Lab files on GitHub: https://github.com/ai-roadmap-365/ai-roadmap-365.github.io/tree/main/labs/sections/programming-with-python/day-045-strings-and-text-processing

  1. Get the hands-on files. Clone the labs repository once (you can reuse this clone for every lesson). This works on macOS, Linux, and Windows (PowerShell or WSL):
    git clone https://github.com/ai-roadmap-365/ai-roadmap-365.github.io.git
    cd ai-roadmap-365.github.io
  2. Open this lesson's lab. Move into the directory for this specific day. Every lab lives at the same predictable path — section / subsection / week / day:
    cd labs/sections/programming-with-python/day-045-strings-and-text-processing
  3. Read the lab guide. Open `README.md` in that directory. It lists the exact commands, what each does, the expected output, and how to check your work — read it before running anything.
  4. Run it and check your work. Follow the README's "How to run" section: run the example first to see the finished result, then complete the numbered exercises in `starter/`, then run the tests. The tests pass (exit 0) only when your work is correct.
    bash tests/run_tests.sh   # or the test command named in the lab README

You can also open the lab as a local page (works offline, shows the file tree and expected output).

Learning objectives

By the end of this lesson you will be able to:

Prerequisites

Why this matters


The idea in plain language

Diagram: str compared with bytes in Python


Historical background


What it is — and what it is not

A string is:

It is not:


Why it was created and what problems it solves


How it works

Creating strings

Indexing and slicing

Diagram: why Python slices are half-open

Diagram: indexing and slicing a Python string with positive and negative positions and one slice highlighted

Essential methods

Diagram: string concatenation in a loop compared with join

MethodWhat it does
.strip()Remove leading and trailing whitespace
.lower() / .upper()Change case
.split(sep)Break into a list; no argument splits on any whitespace
"".join(parts)The inverse — and the right way to build a string
.replace(a, b)Every occurrence, returning a new string
.startswith() / .endswith()A clearer test than slicing
.find() / .index()Position; find returns -1, index raises
inThe readable membership test

Splitting and joining, precisely

Diagram: the differences between Python's string splitting methods

String formatting: three styles

Diagram: the parts of an f-string format specification

name, score = "Ada", 0.9137
f"{name} scored {score:.1%}"        # f-string  — write this
"{} scored {:.1%}".format(name, score)  # str.format — recognise it
"%s scored %.1f%%" % (name, score * 100)  # %-formatting — legacy

Flowchart: an f-string broken into literal text, an embedded expression, and a format spec producing formatted output

Unicode and encoding

Diagram: encoding and decoding between str and bytes

A nod to regular expressions

Diagram: string methods compared with regular expressions


An everyday analogy

A printed page you cannot edit.

The pageThe string
The printed textAn immutable string
Reading the third wordIndexing
Photocopying a passageSlicing
Retyping with a correctionA method returning a new string
The original, unchangedImmutability
A fill-in-the-blanks formAn f-string
Which alphabet it is printed inThe encoding

Examples in practice

Diagram: why string methods must have their result assigned


Implications: security, privacy, performance, scalability, and cost

Diagram: string concatenation compared with parameterised input


Alternatives: free, open source, and commercial

ToolTypeWhere it fits
str methodsFree, built inThe large majority of text work
f-stringsFree, built inAll formatting you write from now on
reFree, standard libraryPatterns — Day 38
textwrapFree, standard libraryWrapping and indenting blocks
unicodedataFree, standard libraryNormalisation, when two strings look identical
str.translateFree, built inFast bulk character mapping

TermWhat it actually is
strImmutable text
bytesRaw bytes, with no encoding attached
EncodingThe rule converting between them
SliceA new string from a range of positions
f-stringA literal with expressions evaluated inside it
Format specWhat follows the colon: width, alignment, precision
Raw stringOne where backslash is not an escape

When to use it — and when not to


The AI connection

Knowledge check

quiz:
  question: "A function calls `text.strip()` on its own line and then returns `text`, but the whitespace is still present. Why?"
  options:
    - "Strings are immutable, so strip() returns a new string and the original is unchanged"
    - "strip() removes only spaces, not tabs or newlines, unless given arguments"
    - "The method must be called as strip(text) to modify the variable in place"
    - "strip() only affects the string if it is assigned to a new variable name"
  answer_index: 0
  explanation: "No string method modifies its target, because no string can be modified at all. Every one returns a new string, so the result has to be assigned — text = text.strip() — or it is computed and immediately discarded."

Hands-on exercise

Slice, transform and format real text, then break an encoding on purpose. Worked through fully in the Day 45 lab directory.

What you are doingCommand
Index from both endss[0], s[-1]
Slice, and reverses[2:5], s[::-1]
Show immutabilitys.strip() without assigning, then with
Build efficiently"".join(parts) rather than += in a loop
Format with a specf"{value:>10.2f}" and f"{ratio:.1%}"
Count characters vs byteslen(s) and len(s.encode())
Cause mojibakes.encode("utf-8").decode("latin-1")

Expected output

'c' 'é'
'af' 'éfac'
'  café  ' 'café'
     12.50  91.4%
4 5
café

Validate your work

Troubleshooting

Common mistakes


Practice assignment


Extension challenge


Quiz

Q1. You run `s = "river"; s.upper()` and then print `s`. What does it show, and why?

  1. "RIVER", because upper() changes the string in place
  2. An error, because strings have no upper() method
  3. "river", because upper() returns a new string and the original is unchanged
  4. "River", because upper() only capitalizes the first letter
Show answer

Answer: C. "river", because upper() returns a new string and the original is unchanged

Strings are immutable, so `upper()` returns a brand-new upper-cased string and leaves `s` untouched. To keep the result you must assign it: `s = s.upper()`.

Q2. What is `len("café")` in Python 3, and what does the number count?

  1. 5, because it counts the bytes needed to store the text
  2. It depends on the operating system
  3. 3, because accented letters are not counted
  4. 4, because it counts characters (code points), not bytes
Show answer

Answer: D. 4, because it counts characters (code points), not bytes

A Python 3 `str` is a sequence of Unicode characters, so `len` counts characters: "café" has 4. Its UTF-8 encoding happens to take 5 bytes because `é` needs two, but `len` on the string counts characters.

Q3. Given `s = "PYTHON"`, what is `s[1:4]`?

  1. "YTH"
  2. "PYT"
  3. "YTHO"
  4. "YTHON"
Show answer

Answer: A. "YTH"

A slice `s[start:stop]` includes `start` but excludes `stop`, so `s[1:4]` takes indices 1, 2, and 3 — "YTH" — which is `4 - 1 = 3` characters long.

Q4. Which formatting style is the recommended default for new Python code?

  1. The `%` operator, e.g. `"Total: %.2f" % price`
  2. The f-string, e.g. `f"Total: {price:.2f}"`
  3. Manual concatenation with `+` and `str()`
  4. The `%` operator for numbers and `+` for text
Show answer

Answer: B. The f-string, e.g. `f"Total: {price:.2f}"`

Introduced in Python 3.6 (PEP 498), f-strings are the modern default: they embed the expression inline, read clearly, and are fast. `%` and `str.format()` still work but are preferred mainly for older code.

Q5. Which operation turns a `str` into `bytes`, and what must you always supply?

  1. `decode`, always supplying a file path
  2. `str()`, which needs nothing
  3. `encode`, always supplying the encoding
  4. `bytes()` with no arguments
Show answer

Answer: C. `encode`, always supplying the encoding

Text is turned into bytes with `encode` (`s.encode("utf-8")`) and bytes back into text with `decode`. You should always name the encoding explicitly rather than rely on the platform default, which is a common cause of `UnicodeDecodeError`.

Q6. What does `"-".join(["a", "b", "c"])` produce?

  1. ["a", "-", "b", "-", "c"]
  2. "abc"
  3. "-a-b-c-"
  4. "a-b-c"
Show answer

Answer: D. "a-b-c"

`join` is a method on the separator: it glues the list elements into one string with the separator placed *between* them, giving "a-b-c". It is the inverse of `split`.

Q7. What is the value of `"river"[::-1]`?

  1. "revir"
  2. "rev ir"
  3. "river"
  4. "revir" reversed to "rivre"
Show answer

Answer: A. "revir"

A slice with a step of `-1`, `s[::-1]`, walks the string backwards and returns a new reversed string, so "river" becomes "revir".

Q8. For which task is a plain string method the better choice over a regular expression?

  1. Matching any run of one or more digits anywhere in the text
  2. Checking whether a filename ends with the fixed suffix ".txt"
  3. Extracting every email-shaped token from a document
  4. Matching whitespace of any kind, including tabs and newlines
Show answer

Answer: B. Checking whether a filename ends with the fixed suffix ".txt"

A fixed-substring test like "ends with .txt" is exactly what `str.endswith(".txt")` is for — simple and readable. The other three involve variable patterns, which are where regular expressions (Day 38) earn their added complexity.

Glossary

string
A piece of text in Python: an ordered, immutable sequence of Unicode characters, written between quotes and represented by the built-in type str.
immutable
Unable to be changed in place. A string never changes; every operation that seems to modify it actually returns a new string and leaves the original untouched.
index
The position of a single character in a string, counted from 0 at the left; negative indices count from the right, so s[-1] is the last character.
slice
A run of characters copied out of a string with s[start:stop], returning a new string that includes start but excludes stop (stop − start characters).
method
A named action called on a string with a dot, such as s.lower() or s.split(); because strings are immutable, transforming methods return a new string.
f-string
A string literal prefixed with f whose {...} braces embed expressions evaluated at run time, the modern default for formatting (added in Python 3.6 via PEP 498).
format spec
The part of an f-string field after the colon that controls display, such as :.2f for two decimal places or :>10 to right-align in a 10-character field.
split
A string method that breaks text into a list of pieces on a separator; with no argument it splits on any run of whitespace.
join
A string method called on a separator that glues a list of strings into one, placing the separator between the pieces; the inverse of split.
encode
The operation that turns a str into bytes using a named encoding such as UTF-8, done at the boundary where text is written to a file or network.
decode
The operation that turns bytes back into a str using a named encoding; the reverse of encode.
escape sequence
A backslash followed by a code that represents a single character hard to type directly, such as \n for a newline or \t for a tab.
raw string
A string literal prefixed with r in which backslashes are treated literally rather than as escapes, useful for file paths and regular-expression patterns.

Sources and further reading


Kept in this browser, no account needed. Your progress page turns the whole record into one link you can bookmark or open on another device.