0
0
Pythonprogramming~5 mins

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

Choose your learning style9 modes available
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.