-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfstring.py
67 lines (48 loc) · 1.37 KB
/
fstring.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
64
65
66
67
import datetime
import timeit
def equals_debugging():
str_value = "一只 åß∂ƒµ🐶"
num_value = 123
print(f'the value is {str_value}')
print(f'{num_value = }')
print(f"num_value = {num_value}")
print(f'debug : {num_value % 2 = }')
def conversions():
str_value = "一只狗 🐶"
print(f'{str_value}') # __str__
print(f'{str_value!s}') # __str__
print(f'{str_value!r}') # !r __repr__
class MyClass:
def __format__(self, format_spec) -> str:
print(f'MyClass __format__ called with {format_spec=!r}')
return "MyClass() ????"
def __repr__(self) -> str:
return "MyClass repr."
def __str__(self) -> str:
return "MyClass str"
def formatting():
num_value = 123.456
now = datetime.datetime.utcnow()
m = MyClass()
print(m)
print(f'{m!s}')
print(f'{m!r}')
print(f'{now=:%Y-%m-%d}')
print(f'{num_value:.2f}')
print(f'{MyClass():blah blah %%MYFORMAT%%}')
nested_format = ".2f"
print(f'{num_value:{nested_format}}')
def s_speed():
timeit.timeit("""name="Saral"
age = 30
'%s is %s.' % (name, age)""", number=1000000)
def f_speed():
timeit.timeit("""name="Saral"
age = 30
f'{name} is {age}.'""", number=1000000)
def main():
# equals_debugging()
# conversions()
formatting()
if __name__ == '__main__':
main()