Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Slice the Phrase | Strings
Tipos de Dados em Python

book
Slice the Phrase

If we are certain that we intend to extract all characters from position 5 to the end of the string, there is no need to employ the string[5 : ending_index] syntax. Python offers a diverse array of built-in functions, and it adjusts its syntax to suit your specific needs.
Let’s look at this phrase:

string = "Push beyond your limits"
phrase = string[5:]

print(phrase)
1234
string = "Push beyond your limits" phrase = string[5:] print(phrase)
copy

Alternatively, this also applies when you require the extraction of a phrase starting from the initial point up to a specified index:

string = "Push beyond your limits"
phrase = string[:11]
print(phrase)
123
string = "Push beyond your limits" phrase = string[:11] print(phrase)
copy
Tarefa

Swipe to start coding

Now it's your turn! Follow these steps:

  1. Use slicing to extract the phrase "Get a foot" from the first string and assign it to the variable phrase1.

  2. Use slicing to extract the phrase "away" from the second string and assign it to the variable phrase2 (using negative indexing is recommended here).

Solução

string1 = "Get a foot in the door in programming!"
string2 = "Plug away"
# Slice out the phrase 'Get a foot'
phrase1 = string1[:10]
# Slice out the phrase 'away'
phrase2 = string2[-4:]

# Print the strings and relevant phrases
print("phrase1 variable equals:", phrase1)
print("phrase2 variable equals:", phrase2)

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 6
string1 = "Get a foot in the door in programming!"
string2 = "Plug away"
# Slice out the phrase 'Get a foot'
phrase1 = string1[___]
# Slice out the phrase 'away'
phrase2 = string2[___:___]

# Print the strings and relevant phrases
print("phrase1 variable equals:", phrase1)
print("phrase2 variable equals:", phrase2)
toggle bottom row
some-alt