Command Palette

Search for a command to run...

Hectal
Phase 3Beginner4 of 18 in Python

Strings and Text in Depth

Slice and search text, use every string method that matters, format output, understand Unicode and bytes, write regular expressions and clean real text.

Almost everything a program touches is text: names, emails, log lines, web pages, file contents, and the prompts and answers of AI models. Phase 1 showed you what a string is. This phase teaches you to work with it like a professional.

You will pick text apart with indexes and slices, use the string methods you will meet every day, format numbers and tables with f-strings, and learn what text really is inside the computer (Unicode code points and bytes), which is where most mysterious bugs with accents, emoji and file encodings come from.

The phase ends with the two big tools of text work: regular expressions, a small language for describing patterns, and a set of habits for cleaning and parsing messy real text such as log files. You will write real code with real output the whole way, with no functions needed yet.

0/7 · 0%
7 topics ~3 h 29 code blocks & diagrams
Start with the first topic
1
3.1

Indexing and Slicing

Every character in a string has a position number, starting at 0, and you can also count from the end with negative numbers. A slice s[start:stop:step] cuts out a piece of the string, never fails for out-of-range numbers, and gives you a new string.

27 min 1 diagram 3 code practice

2
3.2

The String Methods You Use Daily

A method is a built-in action attached to a value, called with a dot: text.strip(). Strings have dozens, and about twenty cover almost everything: trimming, splitting and joining, replacing, searching, changing case, removing prefixes, and checking what a string contains.

27 min 3 code practice

3
3.3

Formatting Text: f-strings in Depth

An f-string is a string with {} holes that Python fills with values, and a small format-spec language after a colon controls width, alignment, number style, percentages and dates. This topic covers the whole language, plus the older str.format and % styles you will meet in other people's code.

28 min 1 diagram 5 code practice

4
3.4

Unicode, Bytes and Encodings

A Python str is a sequence of Unicode code points (numbers that stand for characters), while bytes is a sequence of raw numbers from 0 to 255. Turning text into bytes is encoding and the reverse is decoding; almost every mysterious bug with accents, emoji or é garbage is an encoding mismatch.

28 min 1 diagram 3 code practice

5
3.5

Immutability, Interning and Building Strings Fast

A string can never be changed after it is created: every "change" builds a new string. Knowing this explains why s[0] = "J" fails, why joining pieces beats += in a loop, why you should compare strings with == and never is, and when io.StringIO is the right buffer.

27 min 4 code practice

6
3.6

Regular Expressions with re

A regular expression (regex) is a small language for describing a pattern of text, such as "four digits, a dash, two digits". The re module finds, checks, extracts and replaces text by pattern, using raw strings so backslashes reach the regex engine untouched.

30 min 1 diagram 3 code practice

7
3.7

Real Text Work: Cleaning and Parsing

Real text is messy: stray spaces, mixed case, punctuation, odd line endings. This topic puts the whole phase together: split text into lines and words, clean it, count words, wrap it with textwrap, use the string constants, and parse a small log file.

26 min 1 diagram 3 code practice

Practise this phase

Hands-on exercises: write the code, run it, and press Check to have it tested automatically.