Night 16: Name tags
Вы уже прошли этот вечер на Python или псевдокоде. Он засчитывается для обоих языков. При желании можете потренироваться ещё раз.
Tonight: name tags
- Your pet wants a name tag. Tonight you put values inside text.
- You will learn one thing: f-strings.
Words for tonight
| Word | Meaning |
|---|---|
| f-string | a text with an f in front; values can go inside it |
| braces | the brackets { } |
| space | the empty character between words |
Values inside text
- Put
fbefore the first quote. Then write variable names inside{ }.
name = "Kit"
age = 2
print(f"{name} is {age} years old")
- Python puts each value where its braces are.

Commas or an f-string?
| Code | Output |
|---|---|
print(snack, ":", price) |
apple : 3 |
print(f"{snack}: {price}") |
apple: 3 |
- Commas always add a space. In an f-string you choose the spaces.
Maths inside the braces
- Python works out what is inside
{ }first.
age = 2
print(f"Next year: {age + 1}")
print(f"In dog years: {age * 7}")
Do not forget the f
- Without
f, the braces are just normal text:
name = "Kit"
print("{name} is sleepy")
print(f"{name} is sleepy")
- Now make some name tags!
Сложность 2/5 · 140 монет + небольшой бонус за первое завершение сегодня
Read a name, then an age with int(input()). Use an f-string to print Kit is 2 years old.
Нажмите Запустить, чтобы увидеть результат здесь.
Сложность 2/5 · 140 монет + небольшой бонус за первое завершение сегодня
Read a snack and a price. Print a price tag like apple: 3 coins. There is no space before the :.
Нажмите Запустить, чтобы увидеть результат здесь.
Сложность 2/5 · 140 монет + небольшой бонус за первое завершение сегодня
Read a name and an age. Print Next year Kit is 3. Work out age + 1 inside the { }.
Нажмите Запустить, чтобы увидеть результат здесь.
Сложность 3/5 · 200 монет + небольшой бонус за первое завершение сегодня
Read a name, a kind and a number of nights. Print a 3-line card: Name: Kit, then Kind: fox, then Nights: 12/30.
Нажмите Запустить, чтобы увидеть результат здесь.