python3.6【新特性:f-string】PEP 498: Formatted string literals

原文链接:https://blog.csdn.net/s740556472/article/details/81111493

前言

用了这么久的python3.6,今天才知道居然有了这么一个方便的特性,一起来看一下。

官网资料

https://docs.python.org/3.6/whatsnew/3.6.html#whatsnew36-pep498

这里写图片描述

PEP 498 introduces a new kind of string literals: f-strings, or formatted string literals.
Formatted string literals are prefixed with ‘f’ and are similar to the format strings accepted by str.format(). They contain replacement fields surrounded by curly braces. The replacement fields are expressions, which are evaluated at run time, and then formatted using the format() protocol:

翻译如下:

格式化的字符串文字以“f”为前缀,类似于str.format()接受的格式字符串。它们包含由花括号包围的替换字段。替换字段是表达式,在运行时进行评估,然后使用format()协议进行格式化:

工作原理:

The expressions that are extracted from the string are evaluated in the context where the f-string appeared. This means the expression has full access to local and global variables. Any valid Python expression can be used, including function and method calls.

翻译如下:

从字符串中提取的表达式在f字符串出现的上下文中计算。这意味着表达式可以完全访问本地和全局变量。可以使用任何有效的Python表达式,包括函数和方法调用。

与之前的方法对比

3.6之前,我最常用的方法是%-formatting,之前初学python时,也有总结过相应的用法:链接如下:
https://blog.csdn.net/s740556472/article/details/77823072
为什么看到了新特性,觉得非常开心呢?是因为这种老方式的冗余性,给大家看一个之前写过的python脚本,只是为了生成相应的sql语句,就需要拼装好多冗余的%号,如下:

sql = ("insert into student ("
       "id, name, gender, tall, weight)"
       "values("
       " '%s', '%s', '%s', '%s', '%s');" %(id,name,gender,tall,weight)
       )

只有5个字段的表,都要写这么冗余的%s,所以一旦字段很多,那么发生字符串拼接的错误概率就会变大。这样看来并不友好。。。

新特性f-string

单行f

先使用官网以及自己测试的小例子来说明使用方法:代码如下:
使用方法: f'{}'

这种写法比较像java中的el表达式,也是采用了大括号的形式进行读取!

# coding = utf-8

"""
@author: sy

@file: test_f_string.py

@time: 2018/7/19 10:44

@desc: 测试f字符串新特性

"""

def main():
    name = "Fred"
    print(f'He said his name is {name}.')


def sum_a_b(a, b):
    return a + b


if __name__ == '__main__':
    main()
    a = 1
    b = 3
    print('求和的结果为:' + f'{sum_a_b(a,b)}')

看下结果图:
这里写图片描述

多行f

有时候字符串会拼接很长,所以可以折成多行去做处理
代码如下:

def more_line():
    name = 'lisi'
    age = 12
    ajd = 'handsome'

    speaker = f"Hi {name}. " \
              f"You are {age} years old. " \
              f"You are a {ajd} guy!"

效果图如下:

这里写图片描述

性能

f字符串中的f也可以代表“速度快”。

“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.” (Source)

翻译:

“f -string提供了一种方法,可以在字符串文字中嵌入表达式,使用最小的语法。”应该注意的是,f-string实际上是在运行时计算的表达式,而不是一个常量值。在Python源代码中,f-string是一个文本字符串,前缀为f,其中包含括号内的表达式。表达式被替换为它们的值。”(来源)

总结

总而言之,就是python3.6的 f 字符串用法非常好用,在性能上,在容错率上,在外观上,都是更上一层的!所以强力推荐。。。

额外链接好文

一篇外文:
https://realpython.com/python-f-strings/#multiline-f-strings
一篇国内翻译过来的好文章(但是其中翻译有的地方有误解):
http://www.mlln.cn/2018/05/19/python3%20f-string%E6%A0%BC%E5%BC%8F%E5%8C%96%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值