Module 7 · Strings — topic 3 of 22
Negative indexing
Negative indexes count from the end of a string: -1 is the last character, -2 the second last. Learn how negative and positive indexes relate and where negative indexing simplifies code.
Learning objectives
- Read characters from the end with negative indexes
- Convert between negative and positive indexes
- Use negative indexes for suffix checks
Definition
Negative indexes count from the end of a string: -1 is the last character, -2 the second last. Learn how negative and positive indexes relate and where negative indexing simplifies code.
Introduction
Counting from the start is not always convenient. To reach the last character you would need s[len(s) - 1]. Python lets you count from the end instead with negative numbers: s[-1] is the last character, s[-2] the one before it.
Why it matters
File extensions, check digits, last names and trailing punctuation all sit at the end of strings. Negative indexing reaches them directly and works for strings of any length.
Detailed explanation
Negative indexes are especially helpful when strings have different lengths. A list of file names such as "a.py" and "report_final.py" all have the same last character at index -1, even though their positive indexes differ, so one expression works for every name.
The same idea extends to slices in the next lesson: text[-3:] takes the last three characters of any string, however long it is.
-1 refers to the last character, -2 to the second last, down to -len(s), which is the first character.
Any negative index i refers to the same character as positive index len(s) + i. For a 6-character string, -1 is 5 and -6 is 0.
Indexes below -len(s) raise IndexError, just like positive indexes that are too large.
Negative indexing works for all sequences: lists and tuples too.
There is no -0; 0 always means the first character.
Syntax
- -1 is always the last character.
- Count backwards with larger negative numbers.
- -len(text) is the first character.
Code examples
From the end
N O P True
Too far back
-7 is out of range for a 6-letter word
More examples
Checking endings with negative indexes
'Is it ready?': question 'It is ready.': statement 'Ready': no ending punctuation Last four digits:0366
Step-by-step explanation
- s[-1] reads the final character of each sentence regardless of its length.
- The if-elif chain classifies the sentence by its ending.
- card[-4] to card[-1] read the last four digits, the usual masked form of a card number.
- sep="" prints the pieces without spaces in between.
Expected output
Running “Checking endings with negative indexes” prints exactly:
Practical applications
- Payment pages show only the last four digits of a card.
- Text tools check whether a line ends with punctuation.
- Version strings are parsed from the end, such as the last component of 3.12.1.
Best practices
- Use s[-1] instead of s[len(s) - 1].
- Guard against empty strings.
- Use endswith() (Module 7) for multi-character endings.
Common mistakes
Thinking -0 means the last character
Avoid
Prefer
-0 is just 0, the first character.
Negative index on an empty string
Avoid
Prefer
An empty string has no last character either.
Key takeaways
- Negative indexes count from the end; -1 is the last character.
- Index -i is the same as len(s) - i.
- Indexes below -len(s) raise IndexError.
Try it yourself
- 1Print the last three characters of a word using three negative indexes.
- 2Check whether a word starts and ends with the same letter using index 0 and -1.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
17 questions cover the “Strings” module. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhat is printed?
- BeginnerWhat is printed?