How to apply float precision (type specifier) in a conditional f-string?

You could use another f-string for your first condition:

f"Percent profit : {f'{self.percent_profit:.2f}' if True else 'None yet'}"

Admittedly not ideal, but it does the job.


I think the f string within f string answer is as simple as it gets, but if you want a little bit more readability, consider moving the condition outside the f string:

value = f'{self.percent_profit:.2f}' if True else 'No data yet'
print(f"Percent profit : {value}")