String Basics

From a functional perspective, strings can be used to represent just about anything that can be encoded as text or bytes. In the text department, this includes symbols and words (e.g., your name), contents of text files loaded into memory, Internet addresses, Python source code, and so on. Strings can also be used to hold the raw bytes used for media files and network transfers, and both the encoded and decoded forms of non-ASCII Unicode text used in internationalized programs.

You may have used strings in other languages, too. Python’s strings serve the same role as character arrays in languages such as C, but they are a somewhat higher-level tool than arrays. Unlike in C, in Python, strings come with a powerful set of processing tools. Also unlike languages such as C, Python has no distinct type for individual characters; instead, you just use one-character strings.

Strictly speaking, Python strings are categorized as immutable sequences, meaning that the characters they contain have a left-to-right positional order and that they cannot be changed in place. In fact, strings are the first representative of the larger class of objects called sequences that we will study here. Pay special attention to the sequence operations introduced in this chapter, because they will work the same on other sequence types we’ll explore later, such as lists and tuples.

Table 7-1 previews common string literals and operations we will discuss in this chapter. Empty strings are written as a pair of quotation marks (single or double) with nothing in between, and there are a variety of ways to code strings. For processing, strings support expression operations such as concatenation (combining strings), slicing (extracting sections), indexing (fetching by offset), and so on. Besides expressions, Python also provides a set of string methods that implement common string-specific tasks, as well as modules for more advanced text-processing tasks such as pattern matching. We’ll explore all of these later in the chapter.

Table 7-1. Common string literals and operations

Operation

Interpretation

S = ''

Empty string

S = "spam's"

Double quotes, same as single

S = 's\np\ta\x00m'

Escape sequences

S = """...multiline..."""

Triple-quoted block strings

S = r'\temp\spam'

Raw strings (no escapes)

B = b'sp\xc4m'

Byte strings in 2.6, 2.7, and 3.X (Chapter 4, Chapter 37)

U = u'sp\u00c4m'

Unicode strings in 2.X and 3.3+ (Chapter 4, Chapter 37)

S1 + S2

S * 3

Concatenate, repeat

S[i]

S[i:j]

len(S)

Index, slice, length

"a %s parrot" % kind

String formatting expression

"a {0} parrot".format(kind)

String formatting method in 2.6, 2.7, and 3.X

S.find('pa')

S.rstrip()

S.replace('pa', 'xx')

S.split(',')

S.isdigit()

S.lower()

S.endswith('spam')

'spam'.join(strlist)

S.encode('latin-1')

B.decode('utf8')

String methods (see ahead for all 43): search,

remove whitespace,

replacement,

split on delimiter,

content test,

case conversion,

end test,

delimiter join,

Unicode encoding,

Unicode decoding, etc. (see Table 7-3)

for x in S: print(x)

'spam' in S

[c * 2 for c in S]

map(ord, S)

Iteration, membership

re.match('sp(.*)am', line)

Pattern matching: library module

Beyond the core set of string tools in Table 7-1, Python also supports more advanced pattern-based string processing with the standard library’s re (for “regular expression”) module, introduced in Chapter 4 and Chapter 36, and even higher-level text processing tools such as XML parsers (discussed briefly in Chapter 37). This book’s scope, though, is focused on the fundamentals represented by Table 7-1.

To cover the basics, this chapter begins with an overview of string literal forms and string expressions, then moves on to look at more advanced tools such as string methods and formatting. Python comes with many string tools, and we won’t look at them all here; the complete story is chronicled in the Python library manual and reference books. Our goal here is to explore enough commonly used tools to give you a representative sample; methods we won’t see in action here, for example, are largely analogous to those we will.