University Marketing & Communications

February 24, 2011

Python String Match Snippet

By Chris Heiland

This was originally posted on Chris Heiland’s staff blog while he was a member of the Web Team. Chris migrated this post to the Web Team blog before he left because we thought the content still had value to the UW community.

I can’t imagine why this would ever be useful but I wanted to document it somewhere. I had a case where I wanted to match two strings against each other, but I didn’t want to bust out a regex. So instead here is what I did:

#!/bin/env python
#Works if the end of the string varies in length.

string1 = 'matching.base.string.match'
string2 = 'matching.base.string'
match = string1[len(string2):]
print match

This is why I love python, because even strings can be used as arrays.