2018-01-30 探索python f-strings ,这个是什么,what ,how,

http://www.cnblogs.com/lianzhilei/p/7474708.html

2018-01-30 探索python f-strings ,这个是什么,what ,how,_第1张图片

Python开发【笔记】:探索Python F-strings

F-strings

在python3.6.2版本中,PEP 498 提出一种新型字符串格式化机制,被称为“字符串插值”或者更常见的一种称呼是F-strings(主要因为这种字符串的第一个字母是f)

简单了解:

①、F-strings提供了一种明确且方便的方式将python表达式嵌入到字符串中来进行格式化:


import math

radius = 10

pi = math.pi

print(f'Circumference of a circle with radius {radius}:{2*pi*radius}')


# Circumference of a circle with radius 10:62.83185307179586

②、同样的,在F-strings中我们也可以执行函数:


import math

radius = 10

def area_of_circle(radius):

    return 2 * math.pi * radius


print(f'Area of a circle with radius {radius}:{area_of_circle(radius=radius)}')


# Area of a circle with radius 10:62.83185307179586

③、F-strings的运行速度很快。比%-string和str.format()这两种格式化方法都快得多——这两种是最常用的两种字符串格式化的方式。

你可能感兴趣的:(2018-01-30 探索python f-strings ,这个是什么,what ,how,)