Python standard library--string module

Source: Internet
Author: User

The string contains constants and templates for working with text

Constant

Print(String.whitespace)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)"""abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567 890123456789abcdefabcdef01234567! " #$%& ' () *+,-./:;<=>[email protected][\]^_ ' {|} ~0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz! " #$%& ' () *+,-./:;<=>[email protected][\]^_ ' {|} ~     """#The first one is a few blank lines, and the Windows output is a bit problematic

Function

Capwords (): Capitalize the first letter of each word to customize the delimiter between words
' This is a dog ' Print (s) Print  'this-is-a-dog'print(s2)print' - ' ) "" "isa dogthis a dogthis-is-a-dogthis-is-a-dog" ""

Template

Substitute () incoming template variable, no error
Safe_substitute () catch exception, output as-is
Values = {'var':'Boo'}t= String. Template ("""Variable: $var excape: $$ Variable in text: ${var}iable""")Print(T.substitute (values)) T2= String. Template ("$var are here but $missing are not provided")Try:    Print(T2.substitute (values))exceptKeyerror as err:Print('ERROR:', str (ERR))Print(T2.safe_substitute (values))"""Variable:boo Excape: $ Variable in Text:booiableerror: ' missing ' boo are here but $missing is not provided"""

$$ Output $

Custom template classes inherit template classes from a string, can customize variable delimiters, and variable lookup rules

class MyTemplate (String. Template):    '%'    [a-z]+_[a-z]+' 

Formatter

Python standard library--string module

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.