3.1.4 Template String

Source: Internet
Author: User

String substitution in strings is usually formatted with%s, But this format is more complex to use and can be error-prone even for familiar developers. To solve these problems, provide a template class in the string library. Refer to the PEP 292 documentation for details. The template class string primarily uses the replacement string beginning with $ to replace The string that begins with%.

The rules for replacing strings are as follows:

L $$, with two $ to represent the output of a $ character.

L $identifier, use the $ plus identifier to identify the replacement string position and key value. where identifier is the key to the key-value pair in the function.

L ${identifier}, when a string is immediately followed by a replacement string, it needs to be separated by curly braces so that the key of the key-value pair can be identified. For example , ${identifier}abc, because ABC was followed by a replacement string.

A valueerror exception is thrown when a string that begins with $ cannot be replaced during a replacement .

Class String. Template (Template)

constructor is used to construct a replaceable template string.

Substitute (mapping, **kwds)

This function is used to implement the substitution of a placeholder string in a template string, returning a new string. The parameter mapping is an object that can enter a dictionary class, and the parameter kwds is an object that can enter key-value pairs. the name of the string to replace in the string is the same as the key name in the dictionary, or the key name of the key-value pair.

Safe_substitute (mapping, **kwds)

The function parameters and functions are the same as the above substitute function, the only difference is that when looking for a replacement string , when no corresponding replacement string is found, an exception is thrown in the substitute function valueerror,keyerror, cannot continue to replace, and the safe_substitute function will not throw an exception , but you can continue the output, replacing the character string without making any changes.

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 resulting output is as follows:

$ Tom like Apple

$ tomabc:tom like Apple

$ ${who}abc: $who like Apple

3.1.5 string helper function

String.capwords (S, sep=none)

This function uses the str.split () function to split the parameter s string, and then uses the str.capitalize () function to format it. The string becomes the first letter uppercase the remaining letter lowercase format, and finally uses str.join () to connect the formatted substring together; When connected, if the parameter Sep equals None or not, the default is to use a space as the delimiter substring identifier and output character, and the given letter is used as the delimiter to identify 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 results are as follows:

ABC face China Shenzhen

The Quick Brown Fox Jumped over the Lazy Dog.

ABC #Abc #test #Dog


Cai Junsheng qq:9073204 Shenzhen

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

3.1.4 Template String

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.