Skip to chapter content
navan✳THE BUILD LABPYTHON / GUIDE
0/26 read
CHAPTER 08 / 26 · DATA AND TEXT

Strings and careful text editing.

Transform text deliberately while keeping encoding and meaning in mind.

BY NAVAN SAMI5 MIN READTHEORY FIRST
01 / THE BASICS

Start here.

A string is text inside quotes. Python lets you use single or double quotes, and an f-string inserts expression values inside braces. Strings are immutable: the characters in an existing string do not change. Methods such as strip() and replace() return new strings. If raw = ' Ada ', then raw.strip() produces 'Ada', but raw still has its spaces unless you assign the result to a name.

A string is also a sequence, so text[0] reads its first character and text[1:4] slices positions 1 through 3. split(',') divides text at each comma and returns a list of pieces. A separator's join method performs the opposite kind of assembly: ' / '.join(names) creates one string with that separator between names. These operations work well when the separator has a simple, reliable meaning. A CSV file has quoting rules, so a comma inside a quoted field must be handled by the csv module instead.

Text has an encoding when it is stored as bytes in a file or sent across a network. UTF-8 is a common encoding that supports Unicode text; specify it when opening text files to make the conversion predictable. Visible letters do not always correspond to one simple internal unit, and case rules vary across languages. For names and user input, preserve the original text unless your program has a clear reason to change its display form.

02 / THE IDEA

Build the model.

A Python string is an immutable sequence of Unicode characters. Indexing reads one position, slicing produces a new string, and methods such as strip, replace, and split return transformed values rather than changing the original. Use these methods for well-defined text cleanup: remove surrounding whitespace, divide a record, or replace a known marker.

An f-string combines values into readable output. A placeholder inside braces is evaluated at runtime and formatted as text. This is excellent for labels and reports, but it is not a substitute for parameterised SQL or safe HTML handling. Text assembled for another language needs that language's own escaping or parameter system.

03 / UNDER THE SURFACE

How it works.

split and join are inverse-looking tools with different roles. text.split(',') produces pieces; ','.join(pieces) joins an iterable of strings. They are suitable when the separator is a simple known rule. CSV is more complicated because fields can contain commas or quotes, so use csv.reader instead of split(',') for actual CSV files.

Unicode means a visible symbol may not be one simple character in every context. For searching or comparison, decide whether case, accents, and normalization matter. casefold can support case-insensitive matching better than lower for many languages. When reading from a file, specify encoding='utf-8' so the bytes are decoded predictably across machines.

04 / A WORKED EXAMPLE

Read it line by line.

example.py
raw = '  Ada,Grace,Linus  '
names = [part.strip() for part in raw.strip().split(',')]
print(' / '.join(names))

The outer strip removes whitespace at the ends. split(',') creates three pieces. The comprehension strips each piece, giving clean names. join places ' / ' between them and print displays Ada / Grace / Linus. Each method produces a new value; raw still contains its original spaces.

05 / ANOTHER EXAMPLE

See the rule again.

example-2.py
raw_name = '  ADA  '
name = raw_name.strip().title()
print(f'Hello, {name}!')

The output is Hello, Ada! strip removes outer whitespace and title makes a display form. Names in the real world have diverse casing conventions, so do not automatically rewrite a person's stored name unless that is the intended product behavior.

06 / REMEMBER THIS

A mistake to avoid.

For structured formats such as CSV, JSON, or HTML, use a parser. A chain of string replacements can silently break quoted fields, escaped characters, or nested structure.

Finished this chapter?
Built with curiosity. By Navan Sami. ↗Python · Understand the idea, then write the code.