Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python string formatting

Tags:

python

I see you guys using

url = '"%s"' % url # This part

>>> url = "http://www.site.com/info.xx"
>>> print url
http://www.site.com/info.xx
>>> url = '"%s"' % url
>>> print url
"http://www.site.com/info.xx"

Is it advanced Python? Is there a tutorial for it? How can I learn about it?

like image 941
localhost Avatar asked Feb 12 '09 21:02

localhost


2 Answers

It's common string formatting, and very useful. It's analogous to C-style printf formatting. See String Formatting Operations in the Python.org docs. You can use multiple arguments like this:

"%3d\t%s" % (42, "the answer to ...")
like image 191
dwc Avatar answered Oct 26 '22 22:10

dwc


That line of code is using Python string formatting. You can read up more on how to use it here: http://docs.python.org/library/stdtypes.html#string-formatting

like image 35
Dan Lew Avatar answered Oct 27 '22 00:10

Dan Lew