String slicing in Python
Extracting substrings by position with slice notation.
word = "typestar"
first_four = word[:4]
last_four = word[4:]
middle = word[2:6]
reversed_word = word[::-1]
every_other = word[::2]
last_char = word[-1]
without_ends = word[1:-1]
copy = word[:]
How it works
[:4]and[4:]take the ends;[2:6]the middle.- A negative step like
[::-1]reverses the whole string. - Negative indexes count from the end.
The run, in numbers
- Lines
- 13
- Characters to type
- 196
- Tokens
- 66
- Three-star pace
- 85 tpm
At the three-star pace of 85 tokens a minute, this run takes about 47 seconds.
Step 3 of 9 in Strings, step 9 of 72 in Language basics.