Bird
Raised Fist0
Pythonprogramming~5 mins

String indexing (positive and negative) in Python - Cheat Sheet & Quick Revision

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is string indexing in Python?
String indexing means accessing individual characters in a string using their position number, starting from 0 for the first character.
Click to reveal answer
beginner
How does positive indexing work in Python strings?
Positive indexing starts at 0 for the first character and increases by 1 for each next character to the right.
Click to reveal answer
beginner
What is negative indexing in Python strings?
Negative indexing starts at -1 for the last character and decreases by 1 moving left, allowing easy access from the end.
Click to reveal answer
beginner
Example: What character does 'hello'[-2] return?
It returns 'l' because -1 is 'o', so -2 is the second last character 'l'.
Click to reveal answer
intermediate
What happens if you use an index out of range in string indexing?
Python raises an IndexError because the position does not exist in the string.
Click to reveal answer
What is the index of the first character in a Python string?
ANone
B1
C0
D-1
What does the index -1 represent in a Python string?
ALast character
BSecond character
CFirst character
DSecond last character
If s = 'Python', what is s[3]?
A't'
B'n'
C'o'
D'h'
If s = 'Python', what is s[-4]?
A't'
B'h'
C'o'
D'y'
What error occurs if you try to access s[10] when s = 'Python'?
AValueError
BIndexError
CTypeError
DKeyError
Explain how positive and negative indexing work in Python strings.
Think about counting characters from left and right.
You got /3 concepts.
    What happens if you try to access a string character using an index that is too large or too small?
    Consider what Python does when you ask for something that does not exist.
    You got /3 concepts.

      Practice

      (1/5)
      1.

      What does the index 0 represent in the string "hello"?

      easy
      A. The last letter 'o'
      B. The middle letter 'l'
      C. The first letter 'h'
      D. An error because indexing starts at 1

      Solution

      1. Step 1: Understand positive indexing

        In Python, string indexes start at 0 for the first character.
      2. Step 2: Apply to the string "hello"

        The character at index 0 is the first letter 'h'.
      3. Final Answer:

        The first letter 'h' -> Option C
      4. Quick Check:

        Index 0 = first letter 'h' [OK]
      Hint: Index 0 always means the first letter [OK]
      Common Mistakes:
      • Thinking indexing starts at 1
      • Confusing index 0 with last letter
      • Assuming negative indexes start at 0
      2.

      Which of the following is the correct way to get the last character of a string s in Python?

      easy
      A. s[1]
      B. s[-1]
      C. s[0]
      D. s[len(s)]

      Solution

      1. Step 1: Recall negative indexing

        Negative index -1 refers to the last character in a string.
      2. Step 2: Check each option

        s[1] is second character, s[0] is first, s[len(s)] causes error (index out of range).
      3. Final Answer:

        s[-1] -> Option B
      4. Quick Check:

        Last char = s[-1] [OK]
      Hint: Use -1 to get last character quickly [OK]
      Common Mistakes:
      • Using s[len(s)] causes IndexError
      • Confusing s[1] as last character
      • Forgetting negative indexes start at -1
      3.

      What is the output of this code?

      word = "python"
      print(word[2], word[-3])

      medium
      A. t h
      B. t o
      C. t t
      D. h n

      Solution

      1. Step 1: Find character at index 2

        Index 0='p', 1='y', 2='t'. So word[2] = 't'.
      2. Step 2: Find character at index -3

        Negative indexes count from end: -1='n', -2='o', -3='h'. So word[-3] = 'h'.
      3. Final Answer:

        t h -> Option A
      4. Quick Check:

        word[2] = 't', word[-3] = 'h' [OK]
      Hint: Count from start with positive, from end with negative [OK]
      Common Mistakes:
      • Mixing positive and negative indexes
      • Counting negative indexes starting at 0
      • Confusing letters at indexes 2 and -3
      4.

      Find the error in this code snippet:

      text = "example"
      print(text[-0])

      medium
      A. No error, prints 'e' (same as text[0])
      B. SyntaxError due to invalid index
      C. IndexError because -0 is out of range
      D. IndexError: -0 is invalid

      Solution

      1. Step 1: Understand -0 as index

        In Python, -0 is the same as 0, so text[-0] equals text[0].
      2. Step 2: Check output

        text[0] is 'e', the first letter, so it prints 'e' without error.
      3. Final Answer:

        No error, prints 'e' (same as text[0]) -> Option A
      4. Quick Check:

        -0 = 0 index, prints first letter 'e' [OK]
      Hint: -0 equals 0, so no error and prints first letter [OK]
      Common Mistakes:
      • Thinking -0 is invalid index
      • Expecting IndexError for -0
      • Confusing negative zero with positive zero
      5.

      Given the string s = "programming", which expression returns the substring "ing" using only negative indexes?

      hard
      A. s[-3:-0]
      B. s[-4:-1]
      C. s[-3:-1]
      D. s[-3:]

      Solution

      1. Step 1: Understand slicing with negative indexes

        s[-3:] means start at third last character to end.
      2. Step 2: Apply to "programming"

        Third last character is 'i', so s[-3:] = 'ing'. Other options exclude last character or cause empty slice.
      3. Final Answer:

        s[-3:] -> Option D
      4. Quick Check:

        Negative slice from -3 to end = 'ing' [OK]
      Hint: Use s[-3:] to get last 3 letters easily [OK]
      Common Mistakes:
      • Using s[-3:-1] misses last letter
      • Using s[-4:-1] gives 'min' not 'ing'
      • Using s[-3:-0] is invalid slice