forked from tecladocode/python-refresher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
33 lines (23 loc) · 693 Bytes
/
code.py
File metadata and controls
33 lines (23 loc) · 693 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
name = "Bob"
greeting = "Hello, Bob"
print(greeting)
name = "Rolf"
print(greeting)
greeting = f"Hello, {name}"
print(greeting)
# --
name = "Anne"
print(
greeting
) # This still prints "Hello, Rolf" because `greeting` was calculated earlier.
print(
f"Hello, {name}"
) # This is correct, since it uses `name` at the current point in time.
# -- Using .format() --
# We can define template strings and then replace parts of it with another value, instead of doing it directly in the string.
greeting = "Hello, {}"
with_name = greeting.format("Rolf")
print(with_name)
longer_phrase = "Hello, {}. Today is {}."
formatted = longer_phrase.format("Rolf", "Monday")
print(formatted)