Do you know all the skills of f string in Python
- 2021-12-09 09:41:07
- OfStack
f-string must have been used by many Python users. As a feature introduced in Python3.6, we can embed custom content into strings more conveniently, but the real functions of f-string are far richer than most users know. Today, we will start with get ~
The most basic usage
The basic usage of f-string is very simple, as shown in the following example, in the string prefixed f, directly fill in the value, variable or evaluation expression to be embedded in {}:
print(f'1+1 = {2}')
# Output: 1+1 = 2
a=1 + 1
print(f'1+1 = {a}')
# Output: 1+1 = 2
print(f'1+1 = {1+1}')
# Output: 1+1 = 2
Self-recording expression
Since Python version 3.8, self-recording expressions have been introduced for f-string, so that we can quickly output 1 number of evaluated expressions as follows:
import math
a = 8
print(f'{math. log(8) = }')
# Output: math. log(8) = 2.0794415416798357
Multiline f-string
By wrapping the outermost brackets, we can write multiple lines of f-string very flexibly as follows:
a = 1
b = 2
c = 3
s = (
f'{a = }\n'
f'{b = }\n'
f'{c = }\n'
)
print(s)
# Output:
a = 1
b = 2
c = 3
Formatting Dates in f-string
For date type variables, we can refer to the following way to quickly format them directly in f-string:
import datetime
now = datetime.datetime.now()
print(f'{now:%Y Year %m Month %d No. %H Point %M Points %S Seconds }')
# Output:
2021 Year 10 Month 18 No. 16 Point 45 Points 58 Seconds
Control Floating Point Accuracy
In f-string, we can flexibly control the decimal places of floating-point numbers in the following ways:
import math
pi = math.pi
print(f'{pi: .3f}')
print(f'{pi:.8f}')
# Output
3.142
3.14159265
Standardized display width
When we need to limit the minimum display width of f-string printed content, such as printing a table-like structure, we can refer to the following example:
for x in range(1,11):
print(f'{x:02}|{x**2:3}/{x**5:6}')
# Output
01| 1/ 1
02| 4/ 32
03| 9/ 243
04| 16/ 1024
05| 25/ 3125
06| 36/ 7776
07| 49/ 16807
08| 64/ 32768
09| 81/ 59049
10|100/100000
Modify to left alignment
f-string defaults to right alignment, as in the example above, but we can use it as follows < Change the display mode to left alignment:
for x in range(1,11):
print(f'{x:<2}|{x**2:<3}|{x**5:<6}')
# Output
1 |1 |1
2 |4 |32
3 |9 |243
4 |16 |1024
5 |25 |3125
6 |36 |7776
7 |49 |16807
8 |64 |32768
9 |81 |59049
10|100|100000
Format scientific notation
We can refer to the following ways to realize the scientific notation display mode of specified digits:
import math
pi = math.pi
print(f'{pi*100:.10e}')
# Output
3.1415926536e+02
Control the number of significant digits
We can also control the number of significant digits of the displayed number in the following way, that is, the number displayed from the first number that is not 0 from the left to the right. When the number of digits is lower than the integer part, it will automatically become scientific notation format:
a=1312.3123123123123
print(f'{a:.10g}')
# Output: 1312.312312
a=1312.3123123123123
print(f'{a:.3g}')
# Output: 1.31e+03
Will it be?