python 的print(f"")

python 的print(f"")

个人感觉这个操作比较方便的是里面可以直接传入一个函数


In [2]: for i in range(3):
   ...:     print(f"The {i} row is {i*i}")
   ...:
The 0 row is 0
The 1 row is 1
The 2 row is 4

还可以这样


In [3]: def fun(num):
   ...:     return num*(num+1)/2
   ...:

In [4]: for i in range(3):
   ...:     print(f"The {i} row is {fun(i)}")
   ...:
   ...:
The 0 row is 0.0
The 1 row is 1.0
The 2 row is 3.0

看下面的示例,几乎包括经常使用的了


In [7]: for i in range(3):
   ...:     a = i/10 + i/100 + i/1000 + i/10000
   ...:     b = "abcdefghigk"
   ...:     print(f"the {i} row is {a:8.4f} - {a:08.2f} - {a:8.2e} - {a:8.2%} - {a:8.2g} - {b:8s} - {b:8.3s}")
   ...:
   ...:
   ...:
the 0 row is   0.0000 - 00000.00 - 0.00e+00 -    0.00% -        0 - abcdefghigk - abc
the 1 row is   0.1111 - 00000.11 - 1.11e-01 -   11.11% -     0.11 - abcdefghigk - abc
the 2 row is   0.2222 - 00000.22 - 2.22e-01 -   22.22% -     0.22 - abcdefghigk - abc


打赏,谢谢~~

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,多谢支持~

打开微信扫一扫,即可进行扫码打赏哦