String indexing (positive and negative) in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we access characters in a string by their position, it is important to know how fast this operation is.
We want to understand how the time to get a character changes as the string gets longer.
Analyze the time complexity of the following code snippet.
text = "hello world"
char1 = text[2] # positive index
char2 = text[-3] # negative index
print(char1, char2)
This code gets characters from a string using positive and negative positions.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing a single character by index in the string.
- How many times: Twice, but each access is independent and direct.
Getting a character by index does not depend on the string length; it is a direct jump.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 2 |
| 100 | 2 |
| 1000 | 2 |
Pattern observation: The number of operations stays the same no matter how long the string is.
Time Complexity: O(1)
This means accessing a character by index takes the same short time regardless of string length.
[X] Wrong: "Accessing a character by index takes longer if the string is longer."
[OK] Correct: Strings are stored so that each position can be reached directly, so length does not slow down access.
Knowing that string indexing is fast helps you understand how to write efficient code when working with text.
"What if we tried to find a character by searching through the string instead of indexing? How would the time complexity change?"
Practice
What does the index 0 represent in the string "hello"?
Solution
Step 1: Understand positive indexing
In Python, string indexes start at 0 for the first character.Step 2: Apply to the string "hello"
The character at index 0 is the first letter 'h'.Final Answer:
The first letter 'h' -> Option CQuick Check:
Index 0 = first letter 'h' [OK]
- Thinking indexing starts at 1
- Confusing index 0 with last letter
- Assuming negative indexes start at 0
Which of the following is the correct way to get the last character of a string s in Python?
Solution
Step 1: Recall negative indexing
Negative index -1 refers to the last character in a string.Step 2: Check each option
s[1] is second character, s[0] is first, s[len(s)] causes error (index out of range).Final Answer:
s[-1] -> Option BQuick Check:
Last char = s[-1] [OK]
- Using s[len(s)] causes IndexError
- Confusing s[1] as last character
- Forgetting negative indexes start at -1
What is the output of this code?
word = "python" print(word[2], word[-3])
Solution
Step 1: Find character at index 2
Index 0='p', 1='y', 2='t'. So word[2] = 't'.Step 2: Find character at index -3
Negative indexes count from end: -1='n', -2='o', -3='h'. So word[-3] = 'h'.Final Answer:
t h -> Option AQuick Check:
word[2] = 't', word[-3] = 'h' [OK]
- Mixing positive and negative indexes
- Counting negative indexes starting at 0
- Confusing letters at indexes 2 and -3
Find the error in this code snippet:
text = "example" print(text[-0])
Solution
Step 1: Understand -0 as index
In Python, -0 is the same as 0, so text[-0] equals text[0].Step 2: Check output
text[0] is 'e', the first letter, so it prints 'e' without error.Final Answer:
No error, prints 'e' (same as text[0]) -> Option AQuick Check:
-0 = 0 index, prints first letter 'e' [OK]
- Thinking -0 is invalid index
- Expecting IndexError for -0
- Confusing negative zero with positive zero
Given the string s = "programming", which expression returns the substring "ing" using only negative indexes?
Solution
Step 1: Understand slicing with negative indexes
s[-3:] means start at third last character to end.Step 2: Apply to "programming"
Third last character is 'i', so s[-3:] = 'ing'. Other options exclude last character or cause empty slice.Final Answer:
s[-3:] -> Option DQuick Check:
Negative slice from -3 to end = 'ing' [OK]
- Using s[-3:-1] misses last letter
- Using s[-4:-1] gives 'min' not 'ing'
- Using s[-3:-0] is invalid slice
