Function: Contains constants and classes that handle text.
1, Capwords (): Capitalize the first letter of all the words in a string.
1 >>> import string 2 >>> s = " the Quick brown fox jumped over the lazy dog. 3 >>> s 4 the Quick brown fox jumped over the lazy Dog. 5 >>> String.capwords (s) 6 the Quick Brown Fox Jumped over the Lazy Dog. 7 >>>
2. Maketrans (): Create conversion tables, which can be used in conjunction with the translate () method to modify a set of characters to another set of characters, which is more efficient than repeatedly calling replace ().
1>>>Importstring2>>> leet = String.maketrans ('Abegiloprstz','463611092572')3>>> s ='The quick brown fox jumped over the lazy dog'4>>>Prints5 The quick brown fox jumped over the lazy dog6>>>Prints.translate (leet)7Th3 qu1ck 620wn f0x jum93d 0v32 7h3 142y d06
If in turn, we are not getting the original sentence, because the preceding number string ' 463611092572 ' has duplicate numeric characters, when replaced they will be replaced with the last digit for the character,
If 6 will be replaced with ' G ' instead of ' B '
1 >>> leet = String.maketrans ('463611092572',' Abegiloprstz')2'Th3 qu1ck 620wn f0x jum93d 0v32 7h3 142y d06' 3 >>> t.translate (leet)4'thequlck Gzown Fox jumped Ovez the lazy dog'
Let's take a look at what this ' leet ' is. It looks like the ASCLL character set, and some of the numbers in the number 0-9 have been replaced with letters in the corresponding relationship above. So when you translate, the numbers will
be translated into the corresponding character.
1>>>Leet2 '\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b \x1c\x1d\x1e\x1f! " #$%&\ ' () *+,-./olzeasgt8p:; <=>[email protected][\\]^_ ' abcdefghijklmnopqrstuvwxyz{|} ~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\ X9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\ xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\ Xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\ Xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff'
Template
The following example compares a simple template with a similar string concatenation using the% operator.
1 Importstring2 3Values = {'var':'Foo'}4 5t = string. Template ("""6 Variable: $var7 Escape: $$8 Variable in text: ${var}iable9 """)Ten One Print 'TEMPLATE:', T.substitute (values) A -s =""" - Variable:% (Var) s the Escape: percent - Variable in test:% (Var) siable - """ - + Print 'interpolation:', S%Values - +>>> ================================ RESTART ================================ A>>> at TEMPLATE: - Variable:foo - Escape: $ -Variableinchtext:fooiable - - interpolation: in Variable:foo -Escape:% toVariableinchtest:fooiable + ->>>
You can see that the trigger character ($ or%) is written two times to complete the escape. The important difference is that the template does not consider parameter types. The value is converted to a string, and the string is inserted into the result. Not available here
formatting options. For example, there is no way to control the use of several valid digits to represent a floating-point value. This is a benefit: by using the Safe_substitute () method, you can avoid the exceptions that can occur if you fail to provide all the parameter values required by the template.
1 Importstring2 3Values = {'var':'Foo'}4 5t = string. Template ("$var are here but $missing are not provided")6 Try:7 Print 'substitute ():', T.substitute (values)8 exceptKeyerror, err:9 Print 'ERROR:', str (ERR)Ten One Print 'Safe_substitute ():', T.safe_substitute (values) A ->>> ================================ RESTART ================================ ->>> theSubstitute (): ERROR:'missing' -Safe_substitute (): foo isHere but $missing is notprovided ->>>Safe_substitute does not produce an exception
You can modify the string. The default syntax for template, which is to adjust the regular expression pattern that it uses to look up variable names in the template body. A simple practice is to modify the delimiter and Idpattern class properties.
1 Importstring2 3Template_text =" "4 Delimiter: percent5 Replaced:%with_underscore6 ignored:%notunderscored7 " "8 9D = {'With_underscore':'replaced',Ten 'notunderscored':'Not replaced', One } A - classMyTemplate (String. Template): -delimiter ='%' theIdpattern ='[a-z]+_[a-z]+' - -t =MyTemplate (Template_text) - Print 'Modified ID Pattern:' + PrintT.safe_substitute (d) ->>> ================================ RESTART ================================ +>>> A Modified ID Pattern: at -Delimiter:% - replaced:replaced -Ignored:%notunderscored - ->>>modifying delimiters and matching patterns
To make more complex changes, you can override the Pattern property and define a completely new regular expression.
Python standard library--string