Raven Smith
Raven Smith

Reputation: 127

python fstring with formatting

Would it possible to add if statement in fstrings like:

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

where I want to pad single digit row[0] with a zero, if row[0] is anything other than single digit then no padding is applied, thanks!

Upvotes: 1

Views: 1454

Answers (1)

Scott Boston
Scott Boston

Reputation: 153460

IIUC, you want to use string formatting using Format Specification Mini-Language.

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

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

Output:

01
02
10
20
03
04
30
40

Upvotes: 2

Related Questions