3.1.4 template string, 3.1.4 string

Source: Internet
Author: User

3.1.4 template string, 3.1.4 string

Replace strings in strings. % s is generally used for formatting. However, this formatting method is complicated and error-prone even for familiar developers. To solve these problems, a template class is provided in the string library. For more information, see PEP 292. The template class string mainly uses the replacement string starting with $ to replace the string starting with %.

The rules for replacing strings are as follows:

L $, two $ represents a $ character output.

L $ identifier: identifies and replaces the string position and key value with $. Identifier is the key of the key-value pair in the function.

L $ {identifier}. When the string to be replaced is followed by a string, braces must be used to separate the strings to identify the key of the key-value pair. For example, $ {identifier} abc, because abc is followed by the string to be replaced.

When a string starting with $ cannot be replaced during the replacement process, a ValueError exception is thrown.

Class string. Template (template)

The constructor is used to construct a replaceable template string.

 

Substitute (mapping, ** kwds)

This function is used to replace the placeholder string in the template string and return a new string. The mapping parameter can be used to input dictionary objects. The kwds parameter can be used to input key-value pairs. The name of the string to be replaced in the string is the same as that in the dictionary or as that in the key-value pair.

 

Safe_substitute (mapping, ** kwds)

The parameters and functions of this function are the same as those of the above substitute function. The only difference is that when you find a replacement string, the corresponding replacement string is not found, in the substitute function, ValueError and KeyError are thrown and cannot be replaced. In the safe_substitute function, an exception is thrown, but the output can be continued. The replaced string is not changed.

 

Example:

# Template

 

From string import Template

 

S = Template ('$ who like apple ')

Print (s. substitute (who = 'Tom '))

 

S = Template ('$ {who} abc: $ who like apple ')

Print (s. substitute (who = 'Tom '))

Print (s. safe_substitute ())

The output is as follows:

$ Tom like apple

$ Tomabc: Tom like apple

$ {Who} abc: $ who like apple

 

3.1.5 string auxiliary functions

String. capwords (s, sep = None)

This function uses str. split () function to split the parameter s string, and then use str. the capitalize () function is used to format the substring into the first letter and the other letters in the lower-case format. Finally, str is used. join () concatenates formatted substrings. If the sep parameter is set to None or is not given during the connection, a space is used by default to separate the substrings and output characters; otherwise, the given letter is used as the separator for recognition and output.

 

Example:

 

Import string

S = 'abc face CHINA shenzhen'

Print (string. capwords (s ))

 

S = 'the quick brown fox jumped over The lazy dog .'

Print (string. capwords (s ))

 

Print (string. capwords ('abc # abc # test # dog', sep = '#'))

The output result is as follows:

Abc Face China Shenzhen

The Quick Brown Fox Jumped Over The Lazy Dog.

Abc # Test # Dog

 

 


Cai junsheng QQ: 9073204 Shenzhen

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.