Python module string. py

Source: Internet
Author: User
This article mainly introduces the detailed description of the string. py module in Python. This article is very detailed and has some reference value for everyone. let's take a look at it. This article mainly introduces the detailed description of the string. py module in Python. This article is very detailed and has some reference value for everyone. let's take a look at it.

I. usage

String constant:


import stringprint(string.ascii_lowercase)print(string.ascii_uppercase)print(string.ascii_letters)print(string.digits)print(string.hexdigits)print(string.octdigits)print(string.punctuation)print(string.printable)

Result


abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890123456789abcdefABCDEF01234567!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,- ./:;<=>?@[\]^_`{|}~

II. Template class:

In fact, the Template class can be used with formatted strings and also hasformat()Methods can help you better understand them. First, create a python file:string_template.py,

Then write the following content in it:


import stringvalues = {'var': 'foo'}t = string.Template("""Variable : $varEscape  : $$Variable in text: ${var}iable""")print('TEMPLATE:', t.substitute(values))s = """Variable : %(var)sEscape  : %%Variable in text: %(var)siable"""print('INTERPOLATION:', s % values)s = """Variable : {var}Escape  : {{}}Variable in text: {var}iable"""print('FORMAT:', s.format(**values))

Then, enter the following in the python command line:


$ python string_template.py

Result


TEMPLATE:Variable : fooEscape  : $Variable in text: fooiableINTERPOLATION:Variable : fooEscape  : %Variable in text: fooiableFORMAT:Variable : fooEscape  : {}

You can see that all three of them can be used to format strings. Only the modifiers of the three are different. A good thing about the Template class is that it can inherit the class, customize its modifier after instantiation, and define the variable name format as a regular expression.

For example, string_template_advanced.py:


import stringclass MyTemplate(string.Template): delimiter = '%' idpattern = '[a-z]+_[a-z]+'template_text = ''' Delimiter : %% Replaced : %with_underscore Igonred : %notunderscored'''d = { 'with_underscore': 'replaced', 'notunderscored': 'not replaced',}t = MyTemplate(template_text)print('Modified ID pattern:')print(t.safe_substitute(d))

First, explain the python file above. It defines a class MyTemplate, inherits the string Template class, and then reloads its two fields: Delimiter is the modifier and is now specified as '% ', instead of the previous '$ '. Then, idpattern specifies the variable format.

Result


$ python string_template_advanced.pyModified ID pattern: Delimiter : % Replaced : replaced Igonred : %notunderscored

Why is notunderscored not replaced? The reason is that the underscore '_' is specified in idpattern during class definition, and the variable name is not underlined, so it cannot be replaced.

The above is the detailed description of the string. py module in Python. For more information, see other related articles in the first PHP community!

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.