3 ms·
One case where .format comes in handy: your_dict = { "some": "awesome", "from": "out of", "dictionary": "mapping", } "inser
by BerislavLopac 6y ago
One case where .format comes in handy:
your_dict = {
"some": "awesome",
"from": "out of",
"dictionary": "mapping",
}
"insert {some} values {from} a {dictionary}".format(**your_dict)
- OskarS 6y agoThat is a very neat trick indeed, I'll have to remember this one!
- CrLf 6y agoThis can also be achieved using '%': your_dict = { "some": "awesome", "from": "out of", "dictionary": "mapping", } "insert %(some)s values %(from)s a %(dictionary)s" % your_dict WRT .format, '%' is safer to use with untrusted (well, semi-trusted) format strings, whereas .format can lead to code injection (in more insidious ways than with f-strings).
- ericvsmith 6y agoYou'd be better off using "...".format_map(your_dict). This case is exactly why it exists.