[Tutor] Fwd: findall() returns tuples or string?

Charles Leviton charles.leviton at gmail.com
Tue Mar 5 02:22:07 CET 2013


I have some confusion regarding when findall returns a list of strings and
when it returns a list of tuples.
Would appreciate an explanation.

I thought if I "grouped" my match string using parentheses I'd get a list
of tuples but this example below belies my expectation.

s1 = '<td>1</td><td>Michael</td><td>Jessica</td>'
>>> strlist = re.findall(r'<td>(\w{1,})',s1)
>>> strlist
['1', 'Michael', 'Jessica']
>>> strlist = re.findall(r'<td>(\d+)</td><td>(\w+)</td>\<td>(\w+)</td>', s1)
>>> strlist
[('1', 'Michael', 'Jessica')]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130304/e7b03eb7/attachment.html>


More information about the Tutor mailing list