Lesson 8: String slicing & indexing
A string is a sequence of characters, and each character sits at a position (index) starting from 0. You can grab a single character with s[i], a character from the end with a negative index like s[-1], and a slice of several characters with s[a:b] — where b is not included.
A string is like a train of letter-cars, numbered from 0. An index picks one car; a slice picks a continuous stretch of cars.
- index
- The position number of a character in a string, starting at 0; s[0] is the first character.
- negative index
- Counting from the end: s[-1] is the last character, s[-2] the second-to-last.
- slice
- A continuous substring s[a:b]; starts at a and stops before b (b is excluded).