Flash cards
Review the key moves
What is the main idea behind Python - Slicing Strings?
Lesson checks
Practice each idea before moving on
Short Mimo-style checks built from this lesson's code, terms, and sequence.
Which statement best captures the main point of this lesson?
Complete the missing token from the example code.
b = "___, World!"Put the learning moves in the order that makes the concept easiest to apply.
Slicing
You can return a range of characters by using the slice syntax.
Specify the start index and the end index, separated by a colon, to return a part of the string.
Example
b = "Hello, World!"
print(b[2:5])Note
The first character has index 0.
Slice From the Start
By leaving out the start index, the range will start at the first character:
Example
b = "Hello, World!"
print(b[:5])Slice To the End
By leaving out the end index, the range will go to the end:
Example
b = "Hello, World!"
print(b[2:])Negative Indexing
Example
b = "Hello, World!"
print(b[-5:-2])