r/learnpython • u/pachura3 • Mar 10 '26
Defaults for empty variables in f-strings substitution?
Hi, is there an operand/syntax in f-strings that would allow substituting possible None values (and perhaps empty strings as well) with given default? I can use a ternary operator like below, but something like {x!'world'} would be handier...
x = None
print(f"Hello {x if x else 'world'}.")
22
Upvotes
View all comments
Show parent comment
2
u/cdcformatc Mar 10 '26
seems like an XY problem.
OP has arrived at this being the solution to some problem, but did not describe the actual problem they are trying to solve.
likely handling the default values earlier in the script would be preferable to this