Introduction to the string.py of Python modules

Source: Internet
Author: User

This article describes the string.py of the Python module

Usage

    1. String constants:

      Import string

      Print (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)

Results

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

1.Template class:

In fact, the template class, can be compared with the use of formatted strings and the format () method of the string object, can help to better understand. First, create a new Python file: string_template.py,
Then write the following in the text:

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, on the Python command line, enter:

$ python string_template.py

Results

Template:variable        : Fooescape          : $Variable in text:fooiableINTERPOLATION:Variable        : Fooescape          :% Variable in text:fooiableFORMAT:Variable        : Fooescape          : {}

You can see that all three can play a formatting effect on a string. Only the modifier of the three is different. The good thing about the template class is that it can be instantiated by inheriting the class, customizing its modifiers, and also defining a regular expression for the name format of the variable. As string_template_advanced.py example:

Import string
Class MyTemplate (String. Template):    delimiter = '% '    idpattern = ' [a-z]+_[a-z]+ ' template_text = ' '    delimiter: percent    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 above Python file. It defines a class MyTemplate, inherits the template class of string, and then overloads its two fields: delimiter is the modifier and is now specified for '% ' instead of the previous ' $ '. Next, Idpattern is the format of the variable specified.

Results

$ python string_template_advanced.pymodified ID pattern:    Delimiter:%    replaced  : Replaced    igonred   :%notunderscored

Why is notunderscored not replaced? The reason is that when we define the class, we specify the underscore ' _ ' in Idpattern, and the variable name is not underlined, so it cannot be replaced.

Related Article

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.