f-string

作者: 粟米一粒 | 来源:发表于2019-06-23 10:58 被阅读0次

    f-string

    python3.6 引入了一种新的字符串常量:f-tring或者叫格式化字符串

    f-string使用f作为前缀,和str.format()非常相似,它使用花括号包含的占位符。占位符是一个在运行时计算表达式,然后使用format协议格式化

    >>> name = "Fred"
    >>> f"He said his name is {name}."
    'He said his name is Fred.'
    >>> width = 10
    >>> precision = 4
    >>> value = decimal.Decimal("12.34567")
    >>> f"result: {value:{width}.{precision}}"  # nested fields
    'result:      12.35'
    

    性能

    下图是各种字符串格式化方法的执行时间

    image

    参考

    https://cito.github.io/blog/f-strings/

    https://docs.python.org/3/whatsnew/3.6.html

    相关文章

      网友评论

          本文标题:f-string

          本文链接:https://www.haomeiwen.com/subject/xcdiqctx.html