curriculum/challenges/english/blocks/lecture-introduction-to-python-strings/694147fe940154f20992261b.md
In a previous lesson, you learned how each character in a string can be identified by its index (starting from zero), and accessed using the bracket notation:
my_str = "Hello world"
print(my_str[0]) # H
print(my_str[6]) # w
print(my_str[-1]) # d
String slicing lets you extract a portion of a string or work with only a specific part of it. Here's the basic syntax:
string[start:stop]
If you want to extract characters from a certain index to another, you just separate the start and stop indices with a colon:
my_str = 'Hello world'
print(my_str[1:4]) # ell
Note that the stop index is non-inclusive, so [1:4] just extracted the characters from index 1, and up to, but not including, the character at index 4.
You can also omit the start and stop indices, and Python will default to 0 or the end of the string, respectively. For example, here's what happens if you omit the start index:
my_str = 'Hello world'
print(my_str[:7]) # Hello w
This extracts everything from index 0 up to (but not including), the character at index 7. And here's what happens if you omit the stop index:
my_str = 'Hello world'
print(my_str[8:]) # rld
This extracts everything from the character at index 8 until the end of the string.
Note that slicing a string does not modify the original string:
my_str = 'Hello world'
print(my_str[8:]) # rld
print(my_str) # Hello world
You can also omit both the start and stop indices, which will extract the whole string:
my_str = 'Hello world'
print(my_str[:]) # Hello world
Apart from the start and stop indices, there's also an optional step parameter, which is used to specify the increment between each index in the slice.
Here's the syntax for that:
string[start:stop:step]
In the example below, the slicing starts at index 0, stops before 11, and extracts every second character:
my_str = 'Hello world'
print(my_str[0:11:2]) # Hlowrd
A helpful trick you can do with the step parameter is to reverse a string by setting step to -1, and leaving start and stop blank:
my_str = 'Hello world'
print(my_str[::-1]) # dlrow olleH
How do you extract a specific portion of a string in Python?
Using parentheses with start and end positions.
This operation relies on character positions and uses bracket notation.
Using curly braces with start and stop positions.
This operation relies on character positions and uses bracket notation.
Using square brackets with start and stop positions.
Using angle brackets with start and stop positions.
This operation relies on character positions and uses bracket notation.
3
What is the outcome of 'Hello'[2:]?
llo
lo
Go back to the part of the lesson that talks about omitting start and stop indices.
el
Go back to the part of the lesson that talks about omitting start and stop indices.
l
Go back to the part of the lesson that talks about omitting start and stop indices.
1
What is the purpose of the optional step parameter in string slicing?
It specifies if the sliced string should be changed in place or not.
Go back to the part of the lesson that talks about the step parameter.
It specifies the increment between each character to extract.
It ensures that all characters are extracted.
Go back to the part of the lesson that talks about the step parameter.
It specifies a maximum number of repeated characters to extract.
Go back to the part of the lesson that talks about the step parameter.
2