String Indexing and Slicing in Python#
š Data Analysis Using Python š Strings & Data Structures Lesson 017
ā Previous Ā· Next ā¶ Ā· ā Section Ā· ā Hub
Important
⨠AI-generated content. This page was written with the assistance of an AI language model and is provided as a learning aid. Despite careful review, it may still contain mistakes, omissions, or out-of-date information. Whether you are new to the topic, a team lead, or a senior practitioner, treat it as a starting point rather than an authoritative reference: read it critically and independently verify anything you act on (code, commands, figures, and factual claims) against official documentation and primary sources before relying on it.
Reaching into strings by position#
A string is a sequence of characters, and Python lets you reach into it by position ā
indexing to get a single character, slicing to get a substring. These are the
Python counterparts of the spreadsheetās LEFT/RIGHT/MID and SQLās SUBSTR,
and they are essential for parsing and extracting from text. This lesson covers indexing
and slicing.
Indexing: single characters#
Each character in a string has an index ā its position, counting from zero:
text = "North"
# 01234
text[0] # "N" ā first character (index 0)
text[1] # "o" ā second character
text[-1] # "h" ā last character (negative counts from the end)
text[-2] # "t" ā second to last
The first character is at index 0 (not 1) ā zero-based indexing, a fundamental and
frequently-tripped-over convention. Negative indices count from the end (-1 is the
last), a convenient Python feature for reaching the end without knowing the length.
Slicing: substrings#
Slicing extracts a range of characters with [start:stop]:
text = "North Region"
text[0:5] # "North" ā characters 0 through 4
text[6:] # "Region" ā from index 6 to the end
text[:5] # "North" ā from the start to index 4
text[-6:] # "Region" ā the last six characters
The slice [start:stop] includes start but excludes stop ā text[0:5] is
characters 0,1,2,3,4, not 5 ā the same exclusive-upper-bound convention as range().
Omitting start means āfrom the beginningā; omitting stop means āto the end.ā Slicing
is how you extract a substring by position ā the flexible text-extraction tool.
Indexing/slicing versus the earlier tools#
These operations mirror the string extraction from earlier sections exactly: text[0:5]
is the spreadsheetās LEFT(text, 5) and SQLās SUBSTR(text, 1, 5); text[-3:] is
RIGHT(text, 3). The concept ā extract characters by position ā is identical; Pythonās
[start:stop] syntax is simply another expression of it, and one that generalises to all
sequences (lists too, as the list lessons show).
The caveat#
Two traps recur. Zero-based indexing ā the first character is index 0 ā means positions
are always one less than the ācountingā number, a persistent source of off-by-one errors;
and the exclusive stop in slicing ([0:5] stops at 4) compounds this. Getting an
index or slice boundary wrong extracts the wrong characters, often silently. The other trap
is indexing past the end of a string (text[100] on a short string), which raises an
error ā though slicing past the end is forgiving (it just stops at the end). As with all
position-based extraction, the discipline is care with the boundaries and awareness that
positions count from zero. The next lesson covers building strings from values.
Hint
See also
Source article Adapted (context, re-expressed) in our own words from: https://insightful-data-lab.com/2023/12/06/string-indexing-and-slicing-in-python/ (insightful-data-lab.com).