1. 程式人生 > >python 字符串格式化,使用f前綴

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

bsp for 位置 版本 pytho python 3.6 ext python3.6

格式化一般用%,但後來推薦用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前綴