www.zhblog.net

Python格式化字符串新语法:f-strings

从Python3.6开始,引入了一个新的格式化字符串方式:f-strings。它与之前的格式化方法相比,更具可读性,代码更简洁,不易出错,并且速度更快。


python之前的格式化字符串方式

在python3.6之前,主要有两种方式格式化字符串:%-formatting 和 str.format()。


%-formatting

这是python最开始内置的格式化方式,但现在它已不被官方推荐使用,因为它容易引发错误。

>>> name = "zh"

>>> "Hello, %s" % name

'Hello, zh'


要想替换多个值可以使用元组

>>> name = "zh"

>>> age = 32

>>> "Hello, %s. You are %s." % (name, age)

'Hello, zh. You are 32.'


%-formatting不友好的方面

上面的代码可读性不错,但是当我们需要在一个长的字符串中替换多个参数,它将变得不可读,且容易出错。

>>> first_name = "z"

>>> last_name = "h"

>>> age = 32

>>> profession = "it"

>>> affiliation = "python"

>>> "Hello, %s %s. You are %s. You are a %s. You were a member of %s." % (first_name, last_name, age, profession, affiliation)

'Hello, z h. You are 32. You are a it. You were a member of python.'


str.format()

这种方式在python2.6中引进,是对%-formatting的改良。它使用普通方法调用的语法,并且可以使用对象__format__()方法进行扩展。

在 str.format() 中,需要替换的字段使用大括号标记

>>> "Hello, {}. You are {}".format(name, age)

'Hello, zh. You are 32'


可以使用索引来改变替换的顺序

>>> "Hello, {1}. You are {0}".format(age, name)

'Hello, zh. You are 32'


如果你想使用参数名称,只需在大括号中填写参数名,这样可读性更强

>>> person = {'name': 'zh', 'age': 32}

>>> "Hello, {name}. You are {age}.".format(name=person['name'], age=person['age'])

'Hello, zh. You are 32.'


当然,使用 ** 解包字典更方便

>>> person = {'name': 'zh', 'age': 32}

>>> "Hello, {name}. You are {age}.".format(**person)

'Hello, zh. You are 32.'


str.format() 于 %-formatting 相比绝对是一种升级,更易使用,代码可读性更强。


新的改良方式:f-strings

一种更简单的方式,要想使用,确保 python 版本是 3.6+。

f-strings是以f开头的字符串,中间需要被替换的字段使用大括号表示。f-strings是运行时解析,所以在大括号中可以使用python表达式。

使用方式与 str.format() 类似

>>> name = "zh"

>>> age = 32

>>> f"Hello, {name}. You are {age}."

'Hello, zh. You are 32.'


大写F也可以

>>> name = "zh"

>>> age = 32

>>> F"Hello, {name}. You are {age}."

'Hello, zh. You are 32.'


使用python表达式

可以直接做些简单的事情

>>> f"{2 * 3}"

'6'


调用函数

>>> def to_lowercase(src):

    return src.lower()





>>> name = "ZH"

>>> f"{to_lowercase(name)} if funny."

'zh if funny.'


也可以直接调用对象的方法

>>> name = "ZH"

>>> f"{name.lower()} is funny."

'zh is funny.'


f-strings 也可以作用在类对象上

>>> class Person:

    def __init__(self, first_name, last_name, age):

        self.first_name = first_name

        self.last_name = last_name

        self.age = age

    def __str__(self):

        return f"{self.first_name} {self.last_name} is {self.age}."

    def __repr__(self):

        return f"{self.first_name} {self.last_name} is {self.age}. Surprise!"





    

>>> person = Person("z", "h", 32)

>>> f"{person}"

'z h is 32.'


正常情况下 __str__() 返回对象可读的信息,__repr__() 返回对象明确的信息,str() 和 repr() 分别对应这两种方法。

f-strings 默认是使用 __str__() 方法,如果你想使用 __repr__() 方法,需要明确指定 !r。

>>> person = Person("z", "h", 32)

>>> f"{person}"

'z h is 32.'

>>> f"{person!r}"

'z h is 32. Surprise!'


多行字符串

在多行字符串中,每行字符串前都必须以f开头

>>> name = "zh"

>>> age = 32

>>> affiliation = "python"

>>> message = (

    f"Hi {name}"

    f"You are {age}"

    f"You were in {affiliation}"

    )

>>> message

'Hi zhYou are 32You were in python'


不加会原样输出

>>> message = (

    f"Hi {name}"

    "You are {age}"

    "You were in {affiliation}"

    )

>>> message

'Hi zhYou are {age}You were in {affiliation}'


当然多行字符串仍然可以使用 \ 

>>> message = f"Hi {name}. " \

      f"You are {age}. " \

      f"You were in {affiliation}."

>>> message

'Hi zh. You are 32. You were in python.'


f-strings 也可以配置 """ 使用

>>> message = f"""

    Hi {name}.

    You are {age}.

    You were in {affiliation}.

    """

>>> message

'\n\tHi zh.\n\tYou are 32.\n\tYou were in python.\n\t'


更快的速度

f-strings 比 %-formatting 和 str.format() 都要快。f-strings 在运行时解析表达式,而不是作为常量值。

运行时,大括号内的表达式被解析求值,然后与 f-strings 字符串中其余部分放一起,并返回。

对比三种方式的速度

>>> import timeit

>>> timeit.timeit("""name = "zh"

age = 32

"%s is %s." % (name, age)""")

0.2921929999999975


>>> timeit.timeit("""name = "zh"

age = 32

"{} is {}".format(name, age)""")

0.27604440000004615


>>> timeit.timeit("""name = "zh"

age = 32

f'{name} is {age}'""")

0.20125310000003083


默认运行次数 number=1000000 ,可以看到 f-strings 时最快的。

总结:    

        你可以继续使用以前的格式化字符串方法,但 f-strings 更容易使用,阅读性更强,并且速度更快,不易出错。如果你的 Python 3.6+ ,你应该尝试用它完成工作。


 

展开阅读全文

评论

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 心情