Speaker: Rick Copeland
- String interpolation
- String methods
- String templates
- Regular expression
'My favorite number is %s' % 42
'My favorite number is %(num)d' % {'num': 42}
import math
'The value of pi %.3f' % math.pi
'The value of pi %-20d' % math.pi # padding with spaces
'The value of pi %.20d' % math.pi # padding with zeros
'Value of pi {0}'.format(math.pi)
split()
text = '''hello world there'''
words = text.split()
join()
'-'.join(words)
strip(): remove front and back spaces
lstrip(): remove only front spaces
rstrip(): remove only back spaces
title(): Captialize the initial of each word
isdigit(): is a number?
index('substr'): the index of the matching substr
find('substr', index=0): find the substr in str, starting from index
rfind('substr', index=0): find the substr in str backwardly, starting from index
str.center(num): 'hello'.center(50)
text.lower(),
text.upper(),
text.swapcase(),
import string
tmpl = string.Template('''
Dear $name,
''')
tmpl.substitute(dictionary)
tmpl.safe_substitute(dictionary)
Side notes:
import StringIO: makes a text like a file.
from collections import defaultdict: make a default value for dict.
from collections import Counter: counting
import re
re.match('quick', text)
match = re.match('.*quick', text)
match.groups()
match = re.match('[a-z]+', text) # longest
match = re.match('[a-z]+?', text)# shortest
m = re.search('brown (?P<animal>[a-z]+) jumped', text)
m.groups()
m.groupdict()
re_time= re.compile('\\d{2}:\\d{2}:\\d{2}')
re_time.match('01:02:03').groups()
re_time = re.compile(r'\d{2}:\d{2}:\d{2}')
re_time = re.compile(r'''
stuff # comments
(\d{2}) # another comments
''', re.VERBOSE)
re.search('quick.brown', text, re.DOTALL | re.VERBOSE) #. matches anything but a newline
re.finditer('\w+', text)
re.sub()
re.split() # split text with pattern
Package Layout
- Build your own modules
__init__.pyto represents it is a moduleif __name__ == '__main__ '# likeint main() ...
Tips:
- When import subpackage, it will also import the parent package.
- When import parent package, it is not guaranteed to to import its sub-modules
- When import as module, it will cache it to make it faster to import it again.