liveBook · Manning

Chapter 6: Strings

What you will learn in chapter 6 of The Quick Python Book, Fourth Edition

Introduces the chapter’s focus on handling and formatting text in Python.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces the chapter’s focus on handling and formatting text in Python.

6.1 Strings as sequences of characters

This section explains how to extract and count characters in strings while distinguishing strings from mutable lists.

Key terms: index, slice notation, len, character, one-character string, empty string, strings, lists, slice, modified

6.2 Basic string operations

This section introduces the operators for combining strings and notes that string multiplication is less commonly useful.

Key terms: string concatenation operator +, string multiplication operator

6.3 Special characters and escape sequences

Introduces Python string escape sequences as backslash-starting sequences used to represent characters such as newline and tab.

Key terms: escape sequences, special characters, backslash, newline character, tab character

6.3.1 Basic escape sequences

This subsection introduces Python’s two-character escape sequences and notes that they apply to both strings and bytes objects.

Key terms: two-character escape sequences, strings, bytes objects

6.3.2 Numeric (octal and hexadecimal) and Unicode escape sequences

This subsection explains how numeric escapes represent ASCII characters and how Unicode escapes represent characters by number or name.

Key terms: ASCII, octal escape sequence, hexadecimal escape sequence, Unicode strings, Unicode names, \N, \u

6.3.3 Printing vs. evaluating strings with special characters

This subsection explains why evaluating and printing a string with special characters look different, and how to prevent print from adding a newline.

Key terms: evaluating, print function, special characters, newline, tab, end parameter, final newline

6.4 String methods

This section introduces how to call Python string methods and why their return values matter for immutable strings.

Key terms: string methods, string object, dot (.), return value, immutable

6.4.1 The split and join string methods

Explains how split and join turn strings into lists of substrings and back, how to choose delimiters, and why join is preferable to repeated concatenation.

Key terms: split, join, delimiter, whitespace, string concatenation, +, splits, None

6.4.2 Converting strings to numbers

This section explains how to convert numeric strings with int and float, including how invalid input and a specified base affect conversion.

Key terms: int, float, ValueError, numeric base, base 6

6.4.3 Getting rid of extra whitespace

This subsection explains how to trim whitespace or specified characters from string ends, and how exact prefix and suffix removal differs.

Key terms: strip, lstrip, rstrip, whitespace, trailing newline, extra parameter, removeprefix, removesuffix, exact matches

6.4.4 String searching

This section explains Python’s simple string-searching methods and when a regular-expression search may be more suitable.

Key terms: find, substring, start, end, rfind, index, rindex, ValueError, count, nonoverlapping

6.4.5 Modifying strings

This section shows how to produce modified strings with replacement, character translation, removal, and other string operations without changing the original string.

Key terms: immutable, replace, substring, newstring, maketrans, translate, translation table, lower, upper, capitalize

6.4.6 Modifying strings with list manipulations

This section shows how to manipulate a string through a list of characters and explains when that approach may be costly.

Key terms: immutable objects, list of characters, "".join(), new string objects, relatively expensive

6.4.7 Useful methods and constants

This subsection introduces string methods for checking characteristics, useful constants in the string module, and the fact that the string operations discussed do not change the original string.

Key terms: string methods, digits, alphabetic characters, uppercase, lowercase, string module, string.whitespace, string.digits, string.hexdigits, string.octdigits

6.5 Converting objects to strings

This section explains how repr and str turn Python objects into strings, why explicit conversion matters for concatenation, and which representation to use for debugging or display.

Key terms: repr, string representation, string concatenation, str, formal string representation, informal string representation

6.6 Using the format method

Introduces Python’s string format method, explaining replacement fields and how to include literal braces.

Key terms: format method, format string, replacement fields, replacement values

6.6.1 The format method and positional parameters

This subsection explains how numbered replacement fields in a format string use positional arguments and how doubled braces remain literal.

Key terms: format method, format string, numbered replacement fields, parameters, braces, escapes, replacement field

6.6.2 The format method and named parameters

This section explains how format selects named replacement values and how named and positional parameters can be used together.

Key terms: format method, named parameters, replacement fields, positional parameters, user[1]

6.6.3 Format specifiers

This subsection shows how format specifiers control the appearance of values inserted into replacement fields.

Key terms: format specifier, replacement field, fill character, alignment, width

6.7 String interpolation with f-strings

This section explains how f-strings insert evaluated Python expressions into strings and support formatting and debugging output.

Key terms: f-strings, Python expressions, curly braces, interpolated, format specifiers, debugging, expression, value

6.8 Formatting strings with %

This section explains how old-style Python string formatting uses % to substitute values into a new string and why it is mainly useful for reading older code.

Key terms: string modulus operator, formatting sequences, %s, str, tuple, old style of string formatting, deprecated

6.8.1 Using formatting sequences

Explains how percent-formatting sequences substitute values into strings and control the appearance of a formatted number.

Key terms: formatting sequence, %s, field width, left-justifies

6.8.2 Named parameters and formatting sequences

This section explains named substitutions in %-formatted strings and the basic output controls available through print.

Key terms: formatting sequences, string modulus operator, dictionary, named key, print function, newline character, console output

6.9 Bytes

This section distinguishes bytes from strings and explains how to convert between them when working with binary data or text.

Key terms: string, bytes object, Unicode characters, binary data, encode, decode, type error

6.10 Preprocessing text

This lab sets out a text-preprocessing task and suggests a memory-conscious, step-by-step way to complete it.

Key terms: normalize, punctuation, splitting the text into words, line by line, one cleaning step at a time

6.10.1 Solving the problem with AI-generated code

This section shows how to specify a text-preprocessing task in an AI prompt and check the generated code.

Key terms: prompt, AI tool, moby_01.txt, moby_01_clean.text

6.10.2 Solutions and discussion

6.11 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 5: Lists, tuples, and sets Chapter 7: Dictionaries →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.