You are on page 1of 3

R Anterior Unidade 4 de 8 S Avançar T

" 100 XP

Exercício – Transformar texto usando métodos de


cadeia de caracteres
5 minutos

Este módulo requer uma área restrita para ser concluído.

Uma área restrita fornece acesso a recursos gratuitos. Não haverá cobranças na sua assinatura pessoal. A área
restrita poderá ser usada somente para concluir o treinamento no Microsoft Learn. O uso por qualquer outro motivo
é proibido e poderá levar à perda permanente do acesso à área restrita.

A Microsoft fornece essa experiência de laboratório e o conteúdo relacionado para fins educacionais. Todas as
informações apresentadas são de propriedade da Microsoft e destinam-se exclusivamente à aprendizagem sobre os
produtos e os serviços abordados neste módulo do Microsoft Learn.

Ativar área restrita

 Tempo de execução Arquivo Editar Exibir  Comentários

 Executar tudo   Kernel   Computação não conectado

    

Exercise: Transform strings


There are several operations you can perform on strings when you manipulate them. In this exercise, you'll use string
methods to modify text with facts about the Moon and then extract information to create a short summary.

This exercise is broken into a series of steps. For each step you will be presented with the goal for the step, followed by
an empty cell. Enter your Python into the cell and run it. The solution for each step will follow each cell.

Parsing interesting facts about the moon


Start by storing the following paragraph in a variable named text :

Interesting facts about the Moon. The Moon is Earth's only satellite. There are several interesting facts
about the Moon and how it affects life here on Earth.
On average, the Moon moves 4cm away from the Earth every year. This yearly drift is not significant enough
to cause immediate effects on Earth. The highest daylight temperature of the Moon is 127 C.

# Enter code below


[4] 

Your code should resemble the following:

text = """Interesting facts about the Moon. The Moon is Earth's only satellite. There are several
interesting facts about the Moon and how it affects life here on Earth. On average, the Moon moves 4cm
away from the Earth every year. This yearly drift is not significant enough to cause immediate effects on
Earth. The highest daylight temperature of the Moon is 127 C."""
Separate the paragraph into sentences
In English each sentence ends with a period. You will use this to break the paragraph into difference sentences. Using the
split method to split the text into sentences by looking for the string . (a period followed by a space). Store the
result in a variable named sentences . Print the result.

# Enter code below


[3] 

['Interesting facts about the Moon', "The Moon is Earth's only satellite", 'There are several
interesting facts about the Moon and how it affects life here on Earth', '\nOn average, the Moon
moves 4cm away from the Earth every year', 'This yearly drift is not significant enough to cause
immediate effects on Earth', 'The highest daylight temperature of the Moon is 127 C.']

Your code should resemble the following:

sentences = text.split('. ')


print(sentences)

Desired output
When you run the cell you should see the following result:

['Interesting facts about the Moon', "The Moon is Earth's only satellite", 'There are several interesting
facts about the Moon and how it affects life here on Earth', '\nOn average, the Moon moves 4cm away from
the Earth every year', 'This yearly drift is not significant enough to cause immediate effects on Earth',
'The highest daylight temperature of the Moon is 127 C.']

Find keywords
You will finish your program by adding the code to find any sentences which mention temperature. Add code to loop
through the sentences variable. For each sentence, search for the word temperature . If the word is found, print the
sentence

Your code should resemble the following:

for sentence in sentences:


if 'temperature' in sentence:
print(sentence)

Desired output
When run, the output should look like the following:

The highest daylight temperature of the Moon is 127 C.

 Sem computação Computação não conectado  Exibição Kernel não conectado


Unidade seguinte: Formato de cadeia de caracteres no Python

Continuar T

You might also like