Chapter 8. Strings and Regular Expressions

Strings are not like integers, floats, and booleans. A string is a sequence, which means it contains multiple values in a particular order. In this chapter we’ll see how to access the values that make up a string, and we’ll use functions that process strings.

We’ll also use regular expressions, which are a powerful tool for finding patterns in a string and performing operations like search and replace.

As an exercise, you’ll have a chance to apply these tools to a word game called Wordle.

A String Is a Sequence

A string is a sequence of characters. A character can be a letter (in almost any alphabet), a digit, a punctuation mark, or whitespace.

You can select a character from a string with the bracket operator. This example statement selects character number 1 from fruit and assigns it to letter:

fruit = 'banana'
letter = fruit[1]
       

The expression in brackets is an index, so called because it indicates which character in the sequence to select. But the result might not be what you expect:

letter
       
'a'
       

The letter with index 1 is actually the second letter of the string. An index is an offset from the beginning of the string, so the offset of the first letter is 0:

fruit[0]
       
'b'
       

You can think of 'b' as the 0th letter of 'banana'—pronounced “zero-eth.”

The index in brackets can be a variable:

i = 1
fruit[i]
       
'a'
       

Or an expression that contains variables and operators:

fruit[i+1]
       
'n'
       

But the value of the index has to be an integer—otherwise ...

Get Think Python, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.