I've run into a fairly simple issue that I can't come up with an elegant solution for.
I'm creating a string using str.format
in a function that is passed in a dict
of substitutions to use for the format. I want to create the string and format it with the values if they're passed and leave them blank otherwise.
Ex
kwargs = {"name": "mark"}
"My name is {name} and I'm really {adjective}.".format(**kwargs)
should return
"My name is mark and I'm really ."
instead of throwing a KeyError
(Which is what would happen if we don't do anything).
Embarrassingly, I can't even come up with an inelegant solution for this problem. I guess I could solve this by just not using str.format
, but I'd rather use the built-in (which mostly does what I want) if possible.
Note: I don't know in advance what keys will be used. I'm trying to fail gracefully if someone includes a key but doesn't put it in the kwargs dict. If I knew with 100% accuracy what keys would be looked up, I'd just populate all of them and be done with it.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…