开发者

Passing kwargs into a python string for rendering rather then using %s/%d...etc

开发者 https://www.devze.com 2023-03-24 02:27 出处:网络
About a week ago I saw a script using kwargs i开发者_如何转开发n the string template. Unfortunately I can\'t re-find that script so I thought I\'d ask to see if anyone knows of a way that I can pass k

About a week ago I saw a script using kwargs i开发者_如何转开发n the string template. Unfortunately I can't re-find that script so I thought I'd ask to see if anyone knows of a way that I can pass kwargs into the string builder rather then just args.

Here is the desired result:

"""%name went to the store.""" %( name = John )

Naturally, this won't work and for the life of me I cannot remember the proper syntax.


"""%(name)s went to the store.""" % {"name": "John"}

or

"""%(name)s went to the store.""" % dict(name="John")
0

精彩评论

暂无评论...
验证码 换一张
取 消