Sub topic 1
Strings as ordered sequences
Prerequisites
This chapter assumes you can do the following:
- Run Python code in a REPL or a script.
- Write a string literal and assign it to a variable.
- Use
print()andlen(). - Read a simple
try/exceptblock.
What "sequence" means
A string in Python is an ordered collection of characters. Python groups types like this under the name sequence. Lists, tuples and ranges are sequences too. A sequence has two properties that matter here:
- Its items sit in a fixed order.
- Each item has a numbered position.
Those two properties are why indexing and slicing work on strings. The rest of this tutorial builds on them.
More precisely, a str is a sequence of Unicode code points. A code point is a number that identifies one character in the Unicode standard. For most everyday text, one code point is one character you see on screen. A later chapter covers the cases where that is not true.
s = "Python"
print(type(s))
print(len(s))This prints <class 'str'> and then 6. The string has six items, so len(s) is 6.
Each item is itself a string
Python has no separate "character" type. When you take one item out of a string, you get another str of length 1.
s = "Python"
first = s[0]
print(first)
print(type(first))
print(len(first))
print(first == "P")The output is P, <class 'str'>, 1 and True. You will see s[0] properly in the next chapter. For now, notice that a one-character string is an ordinary string and supports everything a longer string does.
What the sequence model gives you
Because str is a sequence, several tools work on it without any special string code.
| Operation | Example | Result |
|---|---|---|
| Length | `len("Python")` | `6` |
| Iteration | `for ch in "abc"` | visits `"a"`, `"b"`, `"c"` in order |
| Membership | `"th" in "Python"` | `True` |
| Concatenation | `"Py" + "thon"` | `"Python"` |
| Repetition | `"ab" * 3` | `"ababab"` |
Iteration always follows the order of the string. Membership with in tests for a substring, not only a single character.
s = "Python"
for ch in s:
print(ch)
print("th" in s)
print("on" in s)
print("tn" in s)The loop prints P, y, t, h, o, n, each on its own line. The three membership tests print True, True and False. "tn" is not a substring, because the letters must be adjacent and in order.
Positions come from the order
Order is what lets each character have a position. Python counts positions from 0, so the first character is at position 0 and the last is at position len(s) - 1.
The position is not stored in the string. It is the item's place in the order. If the order changed, the positions would change with it, which is why two strings with the same letters in a different order are different strings.
print("stop" == "pots")
print(sorted("stop") == sorted("pots"))The first line prints False, because the order differs. The second prints True, because sorted() returns each string's letters as a list in alphabetical order, and the two lists match.
Strings are also fixed
Strings are sequences that cannot be changed after creation. This property is called immutability. Operations such as + do not alter a string. They produce a new one.
s = "Python"
t = s + "!"
print(s)
print(t)The output is Python and then Python!. The original s is untouched. A later chapter explains how to build modified strings from slices.
Common mistakes at this stage
- Treating the length as the last position. For
"Python", the length is 6 but the last position is 5. - Expecting a separate character type, as in C or Java. In Python,
"P"is astr. - Using
into test for a pattern. It only tests for an exact substring.
Exercise
What do len(s), "y" in s and type(s[1]) give when s = "Python"? Also, what is the position of the last character?
Show answer
len(s) is 6, "y" in s is True, and type(s[1]) is <class 'str'>, because a single item taken from a string is a length-1 string. The last character is at position 5, which is len(s) - 1.