一文了解python 3 字符串格式化 F-string 用法


Posted in Python onMarch 04, 2020

从python3.6开始,引入了新的字符串格式化方式,f-字符串. 这使得格式化字符串变得可读性更高,更简洁,更不容易出现错误而且速度也更快.

在Python 3.6之前,有两种将Python表达式嵌入到字符串文本中进行格式化的主要方法:%-formatting和str.format()。

在本文后面,会详细介绍f-字符串的用法. 在此之前,让我们先来复习一下python中字符串格式化的方法.

python中传统的字符串格式化方法.

在python3.6之前,我们有两种方式可以用来格式化字符串.

  • 占位符+%的方式
  • str.format()方法

首先复习一下这两种方式的使用方法以及其短板.

占位符+%的方式

这种方式算是第0代字符串格式化的方法,很多语言都支持类似的字符串格式化方法. 在python的文档中,我们也经常看到这种方式.

但是!!! BUT!!!

占位符+%的方式并不是python推荐的方式.

Note The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly). Using the newer formatted string literals, the str.format() interface, or template strings may help avoid these errors. Each of these alternatives provides their own trade-offs and benefits of simplicity, flexibility, and/or extensibility.(Python3 doc)

文档中也说了,这种方式对于元组等的显示支持的不够好. 而且很容易产生错误.

而且不符合python代码简洁优雅的人设...

「如何使用占位符+%的方式」

如果你接触过其他的语言,这种方式使用起来会有一种诡异的亲切感,这种亲切感会让你抓狂,内心会暗暗的骂上一句,艹,又是这德行...(这句不是翻译,是我的个人感觉,从来都记不住那么多数据类型的关键字...)

In [1]: name='Eric'

In [2]: 'Hello,%s'%name

Out[2]: 'Hello,Eric'

如果要插入多个变量的话,就必须使用元组.像这样

In [3]: name='Eric'

In [4]: age=18

In [5]: 'Hello %s,you are %d.'%(name,age)

Out[5]: 'Hello Eric,you are 18.'

「为什么说占位符+%的方式不是最好的办法(个人认为是这种方式是一种最操蛋的操作)」

上面有少量的变量需要插入到字符串的时候,这种办法还行. 但是一旦有很多变量需要插入到一个长字符串中...比如...

In [6]: first_name = "Eric"
 ...: last_name = "Idle"
 ...: age = 74
 ...: profession = "comedian"
 ...: affiliation = "Monty Python"


In [7]: "Hello, %s %s. You are %s. You are a %s. You were a member of %s." % (first_name, last_name, age, profession, affiliation)

Out[7]: 'Hello, Eric Idle. You are 74. You are a comedian. You were a member of Monty Python.'

像上面这个例子,代码可读性就很差了.(对读和写的人都是一种折磨...)

使用str.format()的方式

在python2.6之后,引入了str.format()函数,可以用来进行字符串的格式化. 它通过调用对象的__format__()方法(PEP3101中定义)来将对象转化成字符串.

在str.format()方法中,通过花括号占位的方式来实现变量插入.

In [8]: 'hello,{}. You are {}.'.format(name,age)

Out[8]: 'hello,Eric. You are 74.'

甚至可以给占位符加索引.

In [9]: 'hello,{1}. You are {0}.'.format(age,name)

Out[9]: 'hello,Eric. You are 74.'

如果要在占位符中使用变量名的话,可以像下面这样

In [10]: person={'name':'Eric','age':74}

In [11]: 'hello,{name}. you are {age}'.format(name=person['name'],age=person['age'])

Out[11]: 'hello,Eric. you are 74'

当然对于字典来说的话,我们可以使用**的小技巧.

In [15]: 'hello,{name}. you are {age}'.format(**person)

Out[15]: 'hello,Eric. you are 74'

str.format()方法对于%的方式来说已经是一种很大的提升了. 但是这并不是最好的方式.

「为什么format()方法不是最好的方式」 相比使用占位符+%的方式,format()方法的可读性已经很高了. 但是同样的,如果处理含有很多变量的字符串的时候,代码会变得很冗长.

>>> first_name = "Eric"

>>> last_name = "Idle"

>>> age = 74

>>> profession = "comedian"

>>> affiliation = "Monty Python"

>>> print(("Hello, {first_name} {last_name}. You are {age}. " +

>>>  "You are a {profession}. You were a member of {affiliation}.") \

>>>  .format(first_name=first_name, last_name=last_name, age=age, \

>>>    profession=profession, affiliation=affiliation))
'Hello, Eric Idle. You are 74. You are a comedian. You were a member of Monty Python.'

当然,我们也可以通过字典的方式直接传入一个字典来解决代码过长的问题. 但是,python3.6给我们提供了更便利的方式.

f-字符串,一种新的增强型字符串格式化方式

这种新的方式在PEP498中定义.(原文写到这里的时候,作者可能疯了,balabla说了一长串,冷静的我并没有翻译这些废话...) 这种方式也被叫做formatted string literals.格式化的字符串常亮...ummm...应该是这么翻译吧...

这种方式在字符串开头的时候,以f标识,然后通过占位符{}+变量名的方式来自动解析对象的__format__方法. 如果想了解的更加详细,可以参考python文档

一些简单的例子

「使用变量名作为占位符」

In [16]: name = 'Eric'

In [17]: age=74

In [18]: f'hello {name}, you are {age}'

Out[18]: 'hello Eric, you are 74'

「这里甚至可以使用大写的F」

In [19]: F'hello {name}, you are {age}'

Out[19]: 'hello Eric, you are 74'

你以为这就完了吗?

不!

事情远不止想象的那么简单...

在花括号里甚至可以执行算数表达式

In [20]: f'{2*37}'

Out[20]: '74'

如果数学表达式都可以的话,那么在里面执行一个函数应该不算太过分吧...

In [22]: def to_lowercase(input):
 ...:  return input.lower()
 ...:

In [23]: name = 'ERIC IDLE'

In [24]: f'{to_lowercase(name)} is funny'

Out[24]: 'eric idle is funny'

你以为这就完了吗?

不!

事情远不止想象的那么简单...

这玩意儿甚至可以用于重写__str__()和__repr__()方法.

class Comedian:
 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!"


>>> new_comedian = Comedian("Eric", "Idle", "74")

>>> f"{new_comedian}"'Eric Idle is 74.'

关于__str__()方法和__repr__()方法. 这是对象的两个内置方法.__str()__方法用于返回一个便于人类阅读的字符串. 而__repr__()方法返回的是一个对象的准确释义. 这里暂时不做过多介绍. 如有必要,请关注公众号吾码2016(公众号:wmcoding)并发送str_And_repr

默认情况下,f-关键字会调用对象的__str__()方法. 如果我们想调用对象的__repr__()方法的话,可以使用!r

>>> f"{new_comedian}"
'Eric Idle is 74.'

>>> f"{new_comedian!r}"
'Eric Idle is 74. Surprise!'

更多详细内容可以参考这里

多个f-字符串占位符

同样的,我们可以使用多个f-字符串占位符.

>>> name = "Eric"

>>> profession = "comedian"

>>> affiliation = "Monty Python"

>>> message = (
...  f"Hi {name}. "
...  f"You are a {profession}. "
...  f"You were in {affiliation}."
... )

>>> message
'Hi Eric. You are a comedian. You were in Monty Python.'

但是别忘了,在每一个字符串前面都要写上f

同样的,在字符串换行的时候,每一行也要写上f.

>>> message = f"Hi {name}. " \
...   f"You are a {profession}. " \
...   f"You were in {affiliation}."...

>>> message
'Hi Eric. You are a comedian. You were in Monty Python.'

但是如果我们使用"""的时候,不需要每一行都写.

>>> message = f"""
...  Hi {name}. 
...  You are a {profession}. 
...  You were in {affiliation}.
... """
...

>>> message
'\n Hi Eric.\n You are a comedian.\n You were in Monty Python.\n'

关于f-字符串的速度

f-字符串的f可能代表的含义是fast,因为f-字符串的速度比占位符+%的方式和format()函数的方式都要快.因为它是在运行时计算的表达式而不是常量值.(那为啥就快了呢...不太懂啊...)

❝ “F-strings provide a way to embed expressions inside string literals, using a minimal syntax. It should be noted that an f-string is really an expression evaluated at run time, not a constant value. In Python source code, an f-string is a literal string, prefixed with f, which contains expressions inside braces. The expressions are replaced with their values.”(PEP498)

(官方文档,咱不敢翻,大意就是f-字符串是一个在运行时参与计算的表达式,而不是像常规字符串那样是一个常量值)

在运行时,花括号内的表达式在其自己的作用域内求职,单号和字符串的部分拼接到一起,然后返回.

下面我们来看一个速度的对比.

import timeit

time1 = timeit.timeit("""name = 'Eric'\nage =74\n'%s is %s'%(name,age)""",number=100000)
time2 = timeit.timeit("""name = 'Eric'\nage =74\n'{} is {}'.format(name,age)""",number=100000)
time3 = timeit.timeit("""name = 'Eric'\nage =74\nf'{name} is {age}'""",number=100000)

从结果上看的话,f-字符串的方式速度要比其他两种快.

0.030868000000000007
0.03721939999999996
0.0173276

f-字符串的一些细节问题

「引号的问题」 在f-字符串中,注意成对的引号使用.

f"{'Eric Idle'}"
f'{"Eric Idle"}'
f"""Eric Idle"""
f'''Eric Idle'''

以上这几种引号方式都是支持的. 如果说我们在双引号中需要再次使用双引号的时候,就需要进行转义了. f"The \"comedian\" is {name}, aged {age}."

「字典的注意事项」

在字典使用的时候,还是要注意逗号的问题.

>>> comedian = {'name': 'Eric Idle', 'age': 74}

>>> f"The comedian is {comedian['name']}, aged {comedian['age']}."

>>> f'The comedian is {comedian['name']}, aged {comedian['age']}.'

比如上面两条语句,第三句就是有问题的,主要还是引号引起的歧义.

「花括号」 如果字符串中想使用花括号的话,就要写两个花括号来进行转义. 同理,如果想输出两个花括号的话,就要写四个...

>>> f"{{74}}"'{74}'

>>> f"{{{{74}}}}"

「反斜杠」 反斜杠可以用于转义. 但是!!!BUT!!!在f-字符串中,不允许使用反斜杠.

>>> f"{\"Eric Idle\"}"
 File "<stdin>", line 1
 f"{\"Eric Idle\"}"
      ^SyntaxError: f-string expression part cannot include a backslash

像上面这个的解决办法就是

>>> name = "Eric Idle"

>>> f"{name}"'Eric Idle'

「行内注释」 f-字符串表达式中不允许使用#符号.

总结和参考资料

我们依旧可以使用老的方式进行字符串格式化输出. 但是通过f-字符串,我们现在有了一种更便捷,更快,可读性更高的方式. 根据python教义,Zen of Python:

「there should be one? and preferably only one ?obvious way to do it.」 (编程还编出哲理来了...实在不会翻,有一种醍醐灌顶的感觉,内心浮现一个声音,卧槽!好有道理,仿佛自己升华了,但是仔细想想...这句话到底啥意思呢...)

更多的参考资料(我也只是写在这里,反正我是没有闲心看它的...):

PEP502:String Interpolation - Extended Discussion

PEP 536:Final Grammar for Literal String Interpolation

到此这篇关于一文了解python 3 字符串格式化 F-string 用法的文章就介绍到这了,更多相关python字符串格式化f-string内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python分析nignx访问日志脚本分享
Feb 26 Python
Python中对象迭代与反迭代的技巧总结
Sep 17 Python
Python简单生成8位随机密码的方法
May 24 Python
每天迁移MySQL历史数据到历史库Python脚本
Apr 13 Python
tensorflow: variable的值与variable.read_value()的值区别详解
Jul 30 Python
详解python tkinter教程-事件绑定
Mar 28 Python
Python爬虫之urllib基础用法教程
Oct 12 Python
Python&amp;&amp;GDAL实现NDVI的计算方式
Jan 09 Python
Python图像处理库PIL的ImageGrab模块介绍详解
Feb 26 Python
将pymysql获取到的数据类型是tuple转化为pandas方式
May 15 Python
Python如何读写CSV文件
Aug 13 Python
关于python类SortedList详解
Sep 04 Python
Python正则表达式学习小例子
Mar 03 #Python
python正则表达式实例代码
Mar 03 #Python
python 实现在无序数组中找到中位数方法
Mar 03 #Python
python的列表List求均值和中位数实例
Mar 03 #Python
基于Python数据分析之pandas统计分析
Mar 03 #Python
python 的numpy库中的mean()函数用法介绍
Mar 03 #Python
Python统计学一数据的概括性度量详解
Mar 03 #Python
You might like
joomla实现注册用户添加新字段的方法
2016/05/05 PHP
Yii框架常见缓存应用实例小结
2019/09/09 PHP
YII2框架中ActiveDataProvider与GridView的配合使用操作示例
2020/03/18 PHP
Javascript MD4
2006/12/20 Javascript
JavaScript之自定义类型
2012/05/04 Javascript
利用webqq协议使用python登录qq发消息源码参考
2013/04/08 Javascript
原始XMLHttpRequest方法详情回顾
2013/11/28 Javascript
jQuery的选择器中的通配符使用介绍
2014/03/20 Javascript
jquery得到iframe src属性值的方法
2014/09/25 Javascript
JsRender for index循环索引用法详解
2014/10/31 Javascript
谈一谈js中的执行环境及作用域
2016/03/30 Javascript
ionic js 复选框 与普通的 HTML 复选框到底有没区别
2016/06/06 Javascript
使用vue实现点击按钮滑出面板的实现代码
2017/01/10 Javascript
AngularJS实现使用路由切换视图的方法
2017/01/24 Javascript
Jquery实时监听input value的实例
2017/01/26 Javascript
less简单入门(CSS 预处理语言)
2017/03/08 Javascript
vue一个页面实现音乐播放器的示例
2018/02/06 Javascript
vue单页应用在页面刷新时保留状态数据的方法
2018/09/21 Javascript
在Linux系统上通过uWSGI配置Nginx+Python环境的教程
2015/12/25 Python
flask框架使用orm连接数据库的方法示例
2018/07/16 Python
Python并行分布式框架Celery详解
2018/10/15 Python
PyQt5 窗口切换与自定义对话框的实例
2019/06/20 Python
pytorch 在网络中添加可训练参数,修改预训练权重文件的方法
2019/08/17 Python
python英语单词测试小程序代码实例
2019/09/09 Python
Python实现链表反转的方法分析【迭代法与递归法】
2020/02/22 Python
在python中使用nohup命令说明
2020/04/16 Python
python+requests接口压力测试500次,查看响应时间的实例
2020/04/30 Python
利用Python优雅的登录校园网
2020/10/21 Python
python飞机大战游戏实例讲解
2020/12/04 Python
让IE支持CSS3的不完全兼容方案
2014/09/19 HTML / CSS
科颜氏美国官网:Kiehl’s美国
2017/01/31 全球购物
就业协议书范本
2014/10/08 职场文书
采购部年度工作总结
2015/08/13 职场文书
2015年美容师个人工作总结
2015/10/14 职场文书
青少年法制教育心得体会
2016/01/14 职场文书
MySQL悲观锁与乐观锁的实现方案
2021/11/02 MySQL