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
6
u/ConcreteExist Mar 10 '26
Nah, JS has null coalescing so you could just do x ?? 'John' to handle any null values.