Skip to content

Instantly share code, notes, and snippets.

@ldong
Last active August 29, 2015 14:02
Show Gist options
  • Save ldong/18d7e8c5a0b9b23d1f0a to your computer and use it in GitHub Desktop.
Save ldong/18d7e8c5a0b9b23d1f0a to your computer and use it in GitHub Desktop.

Python Fast Track

Speaker: Rick Copeland

3rd session

String processing and regular expressionss

  • String interpolation
  • String methods
  • String templates
  • Regular expression

String interpolation

'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)

String methods

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(),

String template

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

Regular Expressions

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

Packages and Modules

Package Layout

  • Build your own modules
  • __init__.py to represents it is a module
  • if __name__ == '__main__ ' # like int main() ...

Tips:

  1. When import subpackage, it will also import the parent package.
  2. When import parent package, it is not guaranteed to to import its sub-modules
  3. When import as module, it will cache it to make it faster to import it again.

More modules at here and there.

Python Module of the Week

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment