1 funcitons


In [2]:
import string
s = 'hello, i came from China University of Mining and technology'
print(s)
print(string.capwords(s))


hello, i came from China University of Mining and technology
Hello, I Came From China University Of Mining And Technology

In [3]:
import string
values = {'var':'foo'}
t = string.Template('''
Variable : $var
Escape : $$
Variable in text : ${var}iable
''')
print('Template:', t.substitute(values))


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


In [5]:
s = '''
variable : %(var)s
escape : %%
variable in text :%(var)siable
'''
print('Interploation', s % values)


Interploation 
variable : foo
escape : %
variable in text :fooiable


In [7]:
s = '''
varibale : {var}
Escape : {{}}
Variable in text : {var}iable
'''
print('Fomat:', s.format(**values))


Fomat: 
varibale : foo
Escape : {}
Variable in text : fooiable

using safe_substitute method


In [8]:
import string
values = {'var':'foo'}
t = string.Template('$var is here but $missing is not provided')
print('safe_substitute', t.safe_substitute(values))


safe_substitute foo is here but $missing is not provided

2 Advance Templates


In [9]:
import string
class MyTemplate(string.Template):
    delimiter = '%'
    idpattern = '[a-z]+_[a-z]+'

template_text = '''
Delimiter : %%
Replaced : %with_underscore
Ignore : %notunderscores
'''
d = {
    'with_underscore' : 'replaced',
    'notunderscore' : 'not replaced'
}
t = MyTemplate(template_text)
print('Modified ID pattern')
print(t.safe_substitute(d))


Modified ID pattern

Delimiter : %
Replaced : replaced
Ignore : %notunderscores


In [10]:
import string
t = string.Template('$var')
print(t.pattern.pattern)


    \$(?:
      (?P<escaped>\$) |   # Escape sequence of two delimiters
      (?P<named>[_a-z][_a-z0-9]*)      |   # delimiter and a Python identifier
      {(?P<braced>[_a-z][_a-z0-9]*)}   |   # delimiter and a braced identifier
      (?P<invalid>)              # Other ill-formed delimiter exprs
    )
    

In [12]:
import re
import string
class MyTemplate(string.Template):
    delimiter = '{{'
    pattern = r'''
    \{\{(?:
    (?P<escaped>\{\{)|
    (?P<named>[_a-z][_a-z0-9]*)\}\}|
    (?P<braced>[_a-z][_a-z0-9]*)\}\}|
    (?P<invalid>)
    )
    '''
t = MyTemplate('''
{{{{ 
{{var}}
''')
print('MATCHES:', t.pattern.findall(t.template))
print('Substitude:', t.safe_substitute(var='replacement'))


MATCHES: [('{{', '', '', ''), ('', 'var', '', '')]
Substitude: 
{{ 
replacement

2 Constants


In [14]:
import inspect
import string
def is_str(value):
    return isinstance(value, str)
for name, value in inspect.getmembers(string, is_str):
    if name.startswith('_'):
        continue
    print('%s=%r\n' % (name, value))


ascii_letters='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

ascii_lowercase='abcdefghijklmnopqrstuvwxyz'

ascii_uppercase='ABCDEFGHIJKLMNOPQRSTUVWXYZ'

digits='0123456789'

hexdigits='0123456789abcdefABCDEF'

octdigits='01234567'

printable='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'

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

whitespace=' \t\n\r\x0b\x0c'