syntax - Please explain this Python .format call -


i porting sonos controller written in python language. i'm struggling understand method call doing:

 def __send_command(self, endpoint, action, body):         headers = {             'content-type': 'text/xml',             'soapaction': action         }          soap = soap_template.format(body=body) 

specifically .format method. far can tell, soap, soap_template , body strings.

where:

soap_template = '<s:envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingstyle="http://schemas.xmlsoap.org/soap/encoding/"><s:body>{body}</s:body></s:envelope>' 

and

body = '<u:getpositioninfo xmlns:u="urn:schemas-upnp-org:service:avtransport:1"><instanceid>0</instanceid><channel>master</channel></u:getpositioninfo>' 

could please explain in plain english .format method doing?

python has string formatting. way format strings. (prepare them, put them together)

example:

>>> "hello {name}".format(name="garry") 'hello garry' 

or, better example:

>>> name in ["garry", "inbar"]:     print "hello {name}".format(name=name)   hello garry hello inbar 

in case, soap_template string contains {body} tag in it, , function takes , adds body passed function string.


Comments