-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathf_string_memo.py
57 lines (40 loc) · 1.29 KB
/
f_string_memo.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
# Memo from : https://medium.com/bitgrit-data-science-publication/python-f-strings-tricks-you-should-know-7ce094a25d43
# Formatting numbers
number = 330
print(f"number: {number:.2f}") # decimal
print(f"hex: {number:#0x}") # hex
print(f"binary: {number:b}") # binary
print(f"octal: {number:o}") # octal
print(f"scientific: {number:e}") # scientific
print(f"Number: {number:09}") # number of characters
import datetime
today = datetime.datetime.utcnow()
print(f"datetime : {today}\n")
print(f"date time: {today:%m/%d/%Y %H:%M:%S}")
print(f"date: {today:%m/%d/%Y}")
print(f"time: {today:%H:%M:%S.%f}")
print(f"time: {today:%H:%M:%S %p}")
print(f"time: {today:%H:%M}")
from dataclasses import dataclass
@dataclass
class Person:
name: str
age: int
def __str__(self) -> str:
return f"{self.name} is {self.age} years old"
toto = Person("toto tutu", 69)
print(f"{toto}") # str
print(f"{toto!r}") # repr
# alignment
number = 4
print(f"number is {number:4}") # width of 10
# numbers
for number in range(1, 5):
print(f"the number is {number:{number}}")
left = "left text"
center = "center text!"
right = "right text"
print(f"{left:>20}") # left align
print(f"{center:^20}") # center align
print(f"{right:<20}") # right align
print(f"{left : <20}{center : ^20}{right : >20}")