Home >Backend Development >Python Tutorial >Detailed description of the module string.py in Python

Detailed description of the module string.py in Python

高洛峰
高洛峰Original
2017-03-13 09:30:501602browse

This article mainly introduces the relevant information about the detailed description of the module string.py in Python. The introduction in the article is very detailed and has certain reference value for everyone. Friends who need it can take a look below. Bar.

1. Usage

String constant:


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)

Result


##

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

2. Template class:

In fact, the Template class can be compared with the usage of

formatted string and the format() method of string object, you can Helps to understand better. First, create a new python file: string_template.py,

and then write the following content in it:


import string

values = {&#39;var&#39;: &#39;foo&#39;}

t = string.Template("""
Variable : $var
Escape  : $$
Variable in text: ${var}iable
""")

print(&#39;TEMPLATE:&#39;, t.substitute(values))

s = """
Variable : %(var)s
Escape  : %%
Variable in text: %(var)siable
"""

print(&#39;INTERPOLATION:&#39;, s % values)

s = """
Variable : {var}
Escape  : {{}}
Variable in text: {var}iable
"""

print(&#39;FORMAT:&#39;, s.format(**values))

Then, enter in the python command line:


$ python string_template.py

The result


TEMPLATE:
Variable : foo
Escape  : $
Variable in text: fooiable

INTERPOLATION:
Variable : foo
Escape  : %
Variable in text: fooiable

FORMAT:
Variable : foo
Escape  : {}

can be seen All three can have the effect of formatting strings. It's just that the modifiers of the three are different. The good thing about the Template class is that it can inherit the

class through , customize its modifiers after instantiation, and can also regular expression## the name format of the variable #Definition.

Such as string_template_advanced.py example:


##
import string
class MyTemplate(string.Template):
 delimiter = &#39;%&#39;
 idpattern = &#39;[a-z]+_[a-z]+&#39;


template_text = &#39;&#39;&#39;
 Delimiter : %%
 Replaced : %with_underscore
 Igonred : %notunderscored
&#39;&#39;&#39;


d = {
 &#39;with_underscore&#39;: &#39;replaced&#39;,
 &#39;notunderscored&#39;: &#39;not replaced&#39;,
}

t = MyTemplate(template_text)
print(&#39;Modified ID pattern:&#39;)
print(t.safe_substitute(d))

First, explain the above python file. A class MyTemplate is defined inside, which inherits the Template class of string, and then
overloads

its two fields: Delimiter is the modifier, now specified as '%' instead of the previous '$' . Next, idpattern is the format specification for the variable.

Result

$ python string_template_advanced.py
Modified ID pattern:

 Delimiter : %
 Replaced : replaced
 Igonred : %notunderscored

Why is notunderscored not replaced? The reason is that when we define the class, we specify that the underscore '_' should appear in the idpattern, but the
variable name

does not have an underscore, so it cannot be replaced.

The above is the detailed content of Detailed description of the module string.py in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn