String assignment after Python 2.7

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.