Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele String Slicing in Python | Variables and Types in Python
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Introduction to Python(ihor)

bookString Slicing in Python

Sometimes you need to retrieve multiple elements from a text. This is where slicing comes in handy. Slicing allows us to access a range of elements without modifying the original sequence. To retrieve multiple characters at once, use square brackets ([]) and a colon (:) to specify the start and end indices.

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

12345678
# Initial strings platform_name = "codefinity" greeting_message = "How are you" # Slice strings to extract substrings # Spaces count as characters and have their own indices print(platform_name[0:4], platform_name[6:10]) print(greeting_message[2:5], greeting_message[6:11])
copy

You are given the string "Python" stored in the language variable, extract the substrings "tho" and "on". 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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 8

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Suggested prompts:

Kysy minulta kysymyksiä tästä aiheesta

Tiivistä tämä luku

Näytä käytännön esimerkkejä

bookString Slicing in Python

Pyyhkäise näyttääksesi valikon

Sometimes you need to retrieve multiple elements from a text. This is where slicing comes in handy. Slicing allows us to access a range of elements without modifying the original sequence. To retrieve multiple characters at once, use square brackets ([]) and a colon (:) to specify the start and end indices.

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

12345678
# Initial strings platform_name = "codefinity" greeting_message = "How are you" # Slice strings to extract substrings # Spaces count as characters and have their own indices print(platform_name[0:4], platform_name[6:10]) print(greeting_message[2:5], greeting_message[6:11])
copy

You are given the string "Python" stored in the language variable, extract the substrings "tho" and "on". 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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 8
some-alt