Advanced Slicing
There is a more advanced slicing technique that exists. It allows us to extract all the elements in a specific range with a specific step set.
To set this step, we need to put it as the third number after the second colon as follows: s[start:end:step]
. Let's consider a simple example: given string s
, and we want to understand what will be extracted after the following code execute: s[1:10:2]
. This will extract each second element starting from index 1: 1, 3, 5, 7, 9.
Opgave
Swipe to start coding
Given variable course
with the string String Manipulation in Python
. Extract all the elements from index 5 to index 12 with step 3.
Løsning
9
1
2
3
4
# Variable course
course = "String Manipulation in Python"
# Extract elements in range 5-12 with step 3
print("Extracted elements:", course[5:13:3])
Var alt klart?
Tak for dine kommentarer!
Sektion 1. Kapitel 5
9
1
2
3
4
# Variable course
course = "String Manipulation in Python"
# Extract elements in range 5-12 with step 3
print("Extracted elements:", ___)
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat