Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Slicing | Strings
Introduction to Python | Mobile-Friendly
course content

Kursinnehåll

Introduction to Python | Mobile-Friendly

Introduction to Python | Mobile-Friendly

1. Python Basics
2. Variables
3. Strings

book
Slicing

Good, now you know how to extract a single element from a string. But what if you need to get multiple sequential elements at once? Indeed, you can extract these elements one by one, but it's a bit irrational, isn't it?

Surely it is. To get multiple elements at once, you can use slicing. To do so, pass the starting and ending indices within square brackets divided by the colon : sign. Note that the second index will not be included. For example, if you use [1:5], you will get all the elements with indices from 1 to 4. You can look at the example below.

As you can see, the last index is greater than the index of the last element by 1.

You are given the string "Python" stored within the language variable. You need to get the following fragments using slicing: tho, on. For your convenience, the indices for this string are shown 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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 8

Fråga AI

expand
ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

course content

Kursinnehåll

Introduction to Python | Mobile-Friendly

Introduction to Python | Mobile-Friendly

1. Python Basics
2. Variables
3. Strings

book
Slicing

Good, now you know how to extract a single element from a string. But what if you need to get multiple sequential elements at once? Indeed, you can extract these elements one by one, but it's a bit irrational, isn't it?

Surely it is. To get multiple elements at once, you can use slicing. To do so, pass the starting and ending indices within square brackets divided by the colon : sign. Note that the second index will not be included. For example, if you use [1:5], you will get all the elements with indices from 1 to 4. You can look at the example below.

As you can see, the last index is greater than the index of the last element by 1.

You are given the string "Python" stored within the language variable. You need to get the following fragments using slicing: tho, on. For your convenience, the indices for this string are shown 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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 8
Vi beklagar att något gick fel. Vad hände?
some-alt