Slicing Strings
A string is a sequence of individual characters that can be accessed one at a time using the bracket operator.
A String as a Sequence
A string is not a single indivisible item. It is a sequence of individual characters. Python lets you access those characters one at a time by placing an index inside brackets after the string.
For example, imagine the string "CODE". Its characters can be considered separately: C, O, D, and E. To choose one character, you identify its integer index and use the bracket operator with that index.
Positions Begin at Zero
Python uses zero-based indexing. The first character is at index 0, not index 1. An index represents an offset from the start of the string: the first character has an offset of zero because no characters come before it. The next character has an offset of one.
When you count characters for human communication, you may naturally say first, second, and third. When you use Python indices, translate those positions to 0, 1, and 2.
Choosing One Character
The bracket operator uses the form string_name[index], where index must be an integer. Python uses the integer to select one character from the string.
Selecting a Character from CODE
Find the character selected by the expression "CODE"[2].
Step 1: Map the indices: The characters have zero-based indices: C is at 0, O is at 1, D is at 2, and E is at 3.
Step 2: Read the requested index: The bracket contains the integer 2, so the third character is selected.
Step 3: Identify the character: The character at index 2 is D.
"CODE"[2] selects D.
Fixing the First-Character Error
A common indexing mistake is to assume that the first character is at index 1. In Python, index 1 identifies the second character because one character comes before it at index 0.
Using index 1 when you intend to select the first character.
Python starts string indices at 0, so index 1 is the second character.
Fix:
Use index 0 for the first character.Using a non-integer as an index.
The string index must be an integer.
Fix:
Replace the non-integer index with an integer.
Practice the Mapping
For the string "PYTHON", identify the character at index 0, the character at index 1, and the character at index 4. Then explain why index 1 does not refer to the first character.
Hints
- Begin by writing the indices from 0 upward.
- The first character receives index 0.
- Move one position to the right for each increase in the index.
Checking the Practice Mapping
Map the requested indices in "PYTHON" to their characters.
Index 0: The first character is P.
Index 1: The second character is Y.
Index 4: Counting from zero gives P at 0, Y at 1, T at 2, H at 3, and O at 4.
Index 0 selects P, index 1 selects Y, and index 4 selects O. Index 1 is not the first character because the first character is assigned index 0.
Key Takeaways
- A string is a sequence of individual characters.
- Characters can be accessed one at a time with the bracket operator.
- The form string_name[index] uses an integer index to select a character.
- Python uses zero-based indexing, so the first character is at index 0 and the second character is at index 1.
- A non-integer index, such as a float, raises a TypeError.
Key Takeaways
- A string is a sequence of individual characters that can be accessed separately.
- Use the bracket operator with an integer index to select a character.
- Python counts string positions from zero, not one.
- Index 0 identifies the first character; index 1 identifies the second.
- Using a non-integer as an index raises a TypeError.