how to string format when string have {

Chris Angelico rosuav at gmail.com
Sun Apr 20 18:41:27 EDT 2014


On Mon, Apr 21, 2014 at 8:34 AM, Mariano DAngelo
<marianoa.dangelo at gmail.com> wrote:
> And I want to format like this:
>
> context = {
>  "project_name":project_name,
>  "project_url":project_url,
>  }
>
> nginx_conf.format(**context)
>
>
> but since the string have { i can't.
> Is there a way to solve this?

Are you in full control of the source string? You can escape the
braces as explained here:

https://docs.python.org/3.4/library/string.html#format-string-syntax

If you're not in full control (eg if it comes from a user's input), or
if you don't like the idea of doubling all your braces, you could
switch to percent-formatting, or some other form of interpolation. But
the easiest would be to simply escape them.

ChrisA



More information about the Python-list mailing list