When using Python, you often encounter a long string of strings assigned to a variable. In this long string of strings are often mixed with other variables, and in Python's most commonly used web-processing environment, if there is no good variable assignment method, it is more inconvenient to use.
1, such as the page variable assignment, we fixed an address, want to add parameters in the back of the small tail
Var= "1775294"
Url= "http://qujunorz.blog.51cto.com/6378776/" +var
Use the plus sign method.
2, such as common XML (public number of message format)
TEMPLATE = u "" "<xml><tousername><! [cdata[{target}]]></tousername><fromusername><! [cdata[{source}]]></fromusername><createtime>{time}</createtime><msgtype><! [cdata[text]]></msgtype><content><! [cdata[{content}]]></content></xml> "" "
We want to assign values to target, source, and content.
Method 1: Clumsy little tail, format operator%
TEMPLATE = u "" "<xml><tousername><! [cdata[%s]]></tousername><fromusername><! [cdata[%s]]></fromusername><createtime>{time}</createtime><msgtype><! [cdata[text]]></msgtype><content><! [cdata[%s]]></content></xml> "" "% (target,source,content)
#需要先对这三个变量赋值, not very beautiful.
Reference: (http://www.cnblogs.com/vamei/archive/2013/03/12/2954938.html)
Method 2: The Format function that was transplanted from Python3 is quite useful ~
Result=template.format (target= ' 1234 ', source= ' 12314 ', content= ' 1234 ')
This is not very convenient, and not the corresponding position of the line ~
This article is from the "Danielqu" blog, make sure to keep this source http://qujunorz.blog.51cto.com/6378776/1785327
String assignment after Python 2.7