利用f_string实现字符串格式化(python3.6之后的版本新增)
在字符串的前面加上f前缀,并将变量名称用大括号括起来,Python就可以自动识别并引用变量
#其他前缀
>>> b'hello'
b'hello'
>>> bytes
<class 'bytes'>
>>> r'c:\windows\files'
'c:\\windows\\files'
>>> r'/etc/passwd'
'/etc/passwd'
>>>
#在字符串的前面加上f前缀,并将变量名称用大括号括起来,Python就可以自动识别并引用变量
>>> a = 'teacher'
>>> b = 92.3675
>>> f'hello {a} ,your score is {b}'
'hello teacher ,your score is 92.3675'
>>>
利用f_string指定小数位数
>>> a = 'teacher'
>>> b = 92.3675
>>> f'hello {a} ,your score is {b}'
'hello teacher ,your score is 92.3675'
>>>
>>>
>>> f'hello {a} ,your score is {b:.1f}'
'hello teacher ,your score is 92.4'
>>>
实现等宽输出
>>> year = 2020
>>> month = 1
>>> day = 4
>>>
>>> f'{year}-{month:02d}-{day:02d}'
'2020-01-04'
>>>