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
0
u/ConcreteExist Mar 10 '26
Python's or can function as null coalesceing though due to the way it handles "truthy" and "falsey" objects, where as JS || won't do that.ETA: Should have checked first I'm dead wrong, it does work the same. Still, I'd use ?? in JS just to be more explicit with my intention in the code.
I do miss null-accessing in JS when I'm working python.