-
Notifications
You must be signed in to change notification settings - Fork 0
/
f_strings.py
63 lines (41 loc) · 963 Bytes
/
f_strings.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from math import pi
number = 150
# Print variable name and its value
print(f"{number = }")
# Set number of decimals
print(f"{number = :.2f}")
# Set number of characters
print(f"{number = :09.2f}")
# Scientific notation
print(f"{number = :e}")
# Hex conversion
print(f"hex : {number:#0x}")
# Octal conversion
print(f"octal : {number:o}")
print("\n")
ratio = 1 / 2
print(f"{ratio = }")
# Percentage
print(f"{ratio = :.2%}")
print("\n")
large_number = pi**20
print(f"{large_number = }")
# Put comma in large numbers
print(f"{large_number = :,}")
print("\n")
# Use variable in formatting
for n in range(1, 10):
print(f"π to {n} places is {pi:.{n}f}")
print("\n")
# Value conversion
class Human:
def __init__(self, name: str):
self.name: str = name
def __str__(self):
return self.name
def __repr__(self):
return f'Human("{self.name}")'
me = Human("Loïc")
print(f"{me}")
print(f"{me!s}")
print(f"{me!r}")