Chapter 4. Strings
I always liked strange characters.
Tim Burton
Computer books often give the impression that programming is all about math. Actually, most programmers work with strings of text more often than numbers. Logical (and creative!) thinking is often more important than math skills.
Strings are our first example of a Python sequence. A sequence is an ordered collection of elements. Some of the information that you can get from any sequence are as follows:
-
Whether an element is in the sequence
-
The index of an element by its value
-
The element at a particular index
-
A slice of elements in a given range
-
The length of the sequence
-
The minimum and maximum element values
Other sequences that you’ll see in coming chapters include tuples and lists (Chapter 8) and bytes and bytearrays (Chapter 5).
A Python string is a sequence of characters.
But what’s a character?
It’s the smallest unit in a writing system,
and includes letters, digits, symbols, punctuation,
and even whitespace or directives like line feeds.
A character is defined by its meaning (how it’s used),
not how it looks.
It can have more than one visual representation
(in different fonts),
and more than one character can have the same appearance
(such as the visual H,
which means the H sound in the Latin alphabet
but the Latin N sound in Cyrillic).
This chapter concentrates on making and formatting simple text strings, using ASCII (basic character set) examples. Two important text topics are deferred ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access