Strings and Text

8. Strings and Text#

Strings are sequences of characters, so many sequence ideas from lists and tuples also apply to text.

  • Strings as immutable sequences

  • String methods for cleaning and transforming text

  • Regular expressions for pattern matching

  • Dictionaries and counters for text analysis

  • Random text and Markov-style generation

Chapter Video

This overview introduces strings, string operations, and common string methods for working with text.

Python Tutorial for Beginners 2: Strings - Working with Textual Data (21:11) by Corey Schafer

Hosted on YouTube; property of its original creator, embedded here for educational reference only.

Learning Objectives

By the end of this chapter, you will be able to:

  1. Create, index, slice, and compare strings

  2. Use string methods to clean, search, split, join, and format text

  3. Use regular expressions to find and replace text patterns

  4. Count words and summarize text with dictionaries and Counter

  5. Generate simple random text from word relationships

Chapter Flow

Glossary

#

Term

Explanation

1

String

An immutable sequence of text characters.

2

Escape sequence

A backslash-based notation for characters that are hard to type directly.

3

Raw string

A string literal that treats backslashes mostly as ordinary characters.

4

Indexing

Accessing one character from a string by position.

5

Slicing

Creating a substring from a range of positions.

6

String method

A function called on a string object.

7

Case method

A string method that changes or checks capitalization.

8

Splitting

Breaking one string into a list of smaller strings.

9

Joining

Combining strings from an iterable into one string.

10

String formatting

Building strings that include values from variables or expressions.

11

Docstring

A string at the start of a module, function, class, or method that documents it.

12

Regular expression

A pattern language for matching text.

13

re module

Python’s standard-library module for regular expressions.

14

Match object

An object that stores information about a successful regular expression match.

15

Metacharacter

A character with special meaning in a regular expression.

16

Quantifier

A regular expression symbol that controls how many times a pattern may repeat.

17

Character class

A regular expression pattern that matches one character from a set of choices.

18

Counter

A dictionary-like collection for counting hashable values.

19

Bigram

A sequence of two adjacent words or tokens.

20

Markov analysis

A text-generation approach that predicts the next item from recent context.

21

f-string

A string literal prefixed with f that embeds expressions inside {}.

22

Anchor

A regular expression element, such as ^ or $, that matches a position in the string rather than a character.

23

Word boundary

The position between a word character and a non-word character, matched by \b in a regular expression.

24

Capturing group

A parenthesized part of a regular expression that saves the matched text so it can be extracted or reused.

25

Greedy matching

The default behavior of a quantifier, which matches as many characters as possible.

26

Non-greedy matching

The behavior of a quantifier followed by ?, which matches as few characters as possible.

27

Lookahead

A regular expression assertion, written (?=...) or (?!...), that checks what follows a position without including it in the match.

28

Lookbehind

A regular expression assertion, written (?<=...) or (?<!...), that checks what precedes a position without including it in the match.

29

n-gram

A sequence of n adjacent words or tokens, such as a bigram (two) or a trigram (three).

30

Pseudorandom

Describes numbers produced by a deterministic algorithm that look random, such as those from the random module.

Chapter Overview Slides