...

/

Finding all occurrences of a pattern

Finding all occurrences of a pattern

We'll cover the following...

The first thing this alphametics solver does is find all the letters (A–Z) in the puzzle.

Press + to interact
import re
print (re.findall('[0-9]+', '16 2-by-4s in rows of 8')) #①
#['16', '2', '4', '8']
print (re.findall('[A-Z]+', 'SEND + MORE == MONEY')) #②
#['SEND', 'MORE', 'MONEY']

① The re module is Python’s implementation of regular expressions. It has a nifty function called findall() ...

Access this course and 1400+ top-rated courses and projects.