Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer String Slicing in Python | Variables and Types in Python
Introduction to Python (dev copy)

bookString Slicing in Python

Great, now you've grasped how to pull out a single character from a string. But what if you want to grab several consecutive characters? Sure, you can pick them out individually, but that seems a tad tedious, doesn't it?

It sure does. To retrieve multiple characters in one go, you can use a technique called slicing. For this, employ square brackets and denote the beginning and end indices with a colon : in between. It's crucial to note that the end index isn't included. So, when you use [1:5], you're selecting characters at indices 1 through 4. Check out the example below.

As you'll notice, the end position is always one more than the last character's index you want to include. In the above example, there are 10 positions, but the final index is 9.

Note

Don't forget that spaces count as characters and have their own indices. Refer to the example below for clarity.

123456
# Initial strings site = "codefinity" greeting = "How are you" # Slice strings print(site[0:4], site[6:10]) print(greeting[2:5], greeting[6:11])
copy

Given the string "Python" saved in the language variable, your task is to extract the substrings "tho" and "on". To help, the indices for this string are outlined below.

question-icon

Fill in the blanks to complete the task.

# Initial variable
language = "Python"
# Output the string parts
print(language[:])
print(language[
:])
tho
on

Click or drag`n`drop items and fill in the blanks

Note

Keep in mind that slicing does not include the final index. Therefore, when you use language[2:5], it includes the elements at indices 2, 3, and 4, but excludes the element at index 5.

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 9

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Suggested prompts:

Stel mij vragen over dit onderwerp

Vat dit hoofdstuk samen

Toon voorbeelden uit de praktijk

Awesome!

Completion rate improved to 1.64

bookString Slicing in Python

Veeg om het menu te tonen

Great, now you've grasped how to pull out a single character from a string. But what if you want to grab several consecutive characters? Sure, you can pick them out individually, but that seems a tad tedious, doesn't it?

It sure does. To retrieve multiple characters in one go, you can use a technique called slicing. For this, employ square brackets and denote the beginning and end indices with a colon : in between. It's crucial to note that the end index isn't included. So, when you use [1:5], you're selecting characters at indices 1 through 4. Check out the example below.

As you'll notice, the end position is always one more than the last character's index you want to include. In the above example, there are 10 positions, but the final index is 9.

Note

Don't forget that spaces count as characters and have their own indices. Refer to the example below for clarity.

123456
# Initial strings site = "codefinity" greeting = "How are you" # Slice strings print(site[0:4], site[6:10]) print(greeting[2:5], greeting[6:11])
copy

Given the string "Python" saved in the language variable, your task is to extract the substrings "tho" and "on". To help, the indices for this string are outlined below.

question-icon

Fill in the blanks to complete the task.

# Initial variable
language = "Python"
# Output the string parts
print(language[:])
print(language[
:])
tho
on

Click or drag`n`drop items and fill in the blanks

Note

Keep in mind that slicing does not include the final index. Therefore, when you use language[2:5], it includes the elements at indices 2, 3, and 4, but excludes the element at index 5.

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 9
some-alt