top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

How to string format when string have { in Python?

+3 votes
54 views

I have the following string:

nginx_conf = '''
server {
 listen 80;
 server_name dev.{project_url};

 location / {
 proxy_pass http://127.0.0.1:8080;
 include /etc/nginx/proxy.conf;
 }

 location /media {
 alias /home/mariano/PycharmProjects/{project_name}/{project_name}/media;
 expires 30d;

 }
 location /static {
 alias /home/mariano/PycharmProjects/{project_name}/{project_name}/static;
 expires 30d;
 }

 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 root html;
 }
}'''

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?

posted Apr 20, 2014 by Deepak Dasgupta

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

1 Answer

0 votes

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.

answer Apr 20, 2014 by Deepankar Dubey
Similar Questions
0 votes

This isnt a huge issue, but I am wondering. I am running Mac OS X, I tried to configure with --with-valgrind and this is the error that I got:

configure: error: Valgrind support requested but headers not available

Now, I have valgrind installed, so it should work, yes?If someone has any extra info on this, Id appreciate it.

+1 vote

I read the online help about string. It lists string constants, string formatting, template strings and string functions. After reading these, I am still puzzled about how to use the string module.

Could you show me a few example about this module?

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...