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.

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).

Tags: purpose: reference topic: data analytics topic: python topic: structures