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