python 字符串格式化,使用f前缀

Posted 北风之神0509

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 字符串格式化,使用f前缀相关的知识,希望对你有一定的参考价值。

格式化一般用%,但后来推荐用format

format有进步,可以用索引或者名字,但仍然没有很方便和快捷

 

# logger.debug(‘{}  {}  {}  {}  {}  {}‘.format(method,  resp.status_code, resp.elapsed.total_seconds(), resp.is_redirect, resp.text.__len__(),resp.url))
logger.debug(f‘{method} {resp.status_code} {round(resp.elapsed.total_seconds(),2):>3.2f} {resp.is_redirect} {resp.text.__len__():<8d} {resp.url}‘)

上面的改写为下面的,那么参数的位置信息立马就清晰很多。

前提是要使用python3.6以及以上版本

 

以上是关于python 字符串格式化,使用f前缀的主要内容,如果未能解决你的问题,请参考以下文章

在 Python 格式(f-string)字符串中,!r 是啥意思? [复制]

python的循環之二

python的循環之二

Python字符串加上前缀r|u|b|f

python f-string

Python函数