Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gmolveau
GitHub Repository: gmolveau/python_full_course
Path: blob/master/examples/fstring.py
305 views
1
# literal curly braces
2
print(f"{{ok}}")
3
4
# variable
5
username = "test"
6
print(f"user is {username} - with braces > {{{username}}}")
7
prop = 1/3
8
print(f"{prop} - {prop:.3f}")
9
10
# expression
11
print(f"{2 * 2}")
12
print(f"user has a {'short' if len(username) < 5 else 'long'} username")
13
14
# function
15
16
17
def connect_status(username):
18
return "connected"
19
20
21
log = f"user: {username} is {connect_status(username)}"
22
print(log)
23
24
# multiline print
25
print(
26
f"1"
27
f"2"
28
f"3"
29
)
30
31
# using single and double quotes
32
print(f'''je fais ce'que'je "veux" ok''')
33
34
# raw f-string
35
print(f'this is a not a phase \nmom')
36
print(fr'this is a not a phase \n mom')
37
38
# cool trick using the = operator
39
print(f"{username=}")
40
41
# the ! operator
42
face = "hmmm 🤔"
43
print(f"{face}")
44
print(f"{face!a}") # == convert to ascii
45
print(f"{face!r}") # == repr(face)
46
47