格式化的python fstring

问题描述

是否可以在fstrings中添加if语句:

id1='somestring'
for row,col in <some_condition>:
    col.to_excel(f'{id1}_concat(0,row[0]))

在这里我要用一位填充零位的row [0],如果row [0]不是一位数字,则不应用填充,谢谢!

解决方法

IIUC,您想使用Format Specification Mini-Language使用字符串格式。

ls = [1,2,10,20,3,4,30,40]

for i in ls:
    print(f'{i:02d}')

输出:

01
02
10
20
03
04
30
40

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...