Você está na página 1de 2

R Anterior Unidade 6 de 8 S Avançar T

" 100 XP

Exercício – Formatar cadeias 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: Formatting strings


Knowing how to format strings is essential when you're presenting information from a program. There are a few different
ways to accomplish this in Python. In this exercise, you use variables that hold key facts about gravity on various moons
and then use them to format and print the information.

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.

Create the variables


Start by creating three variables, name , gravity , and planet , and set them to the following values:

name: Ganymede
planet: Mars
gravity: 1.43

# Enter code below


Your code should look like the following:

name = 'Ganymede'
planet = 'Mars'
Create the template
gravity = '1.43'

Now you will create the template to display the information about the moon. You want the output to look like the following

Gravity Facts about {name}


--------------------------
Planet Name: {planet}
Gravity on {name}: {gravity} m/s2

Create a variable named template , and set it to the template you create.

# Enter code below

Your code should look like the following:

template = """Gravity Facts about {name}


----------------------------------------
Planet Name: {planet}
Gravity on {name}: {gravity} m/s2"""

Use the template


With the template created, it's time to use it to display information about the moon! Use the format function on templa
to use the template and print the information. Set name , planet , and gravity to the appropriate values.

Your code should look like the following:

print(template.format(name=name, planet=planet, gravity=gravity))

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

Gravity Facts about Ganymede


--------------------------
Planet Name: Mars
Gravity on Ganymede: 1.43 m/s2

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

Unidade seguinte: Verificação de conhecimentos

Você também pode gostar