How can I use f-string with logic to format an int
as a float
? I would like if ppl
is True
to format num
to 2 decimal places, and if ppl
is False
to rformat it as whatever it is.
Something like string = f'i am {num:.2f if ppl else num}'
but this does not work. The below code demonstrates the behaviour that I want to achieve with a simpler f-string if possible:
ppl = True
num = 3
string = f'I am {num:.2f}' if ppl else f'I am {num}'
print(string)
#if ppl False
#=> i am 3
#if ppl True
#=> i am 3.00