How to extract 2 integers from a string in python?

Stephen Prinster prinster at mail.com
Fri Jun 9 01:13:59 EDT 2006


yinglcs at gmail.com wrote:
> Hi,
> 
> how can I extract 2 integers from a string in python?
> 
> for example, my source string is this:
> Total size: 173233 (371587)
> 
> I want to extract the integer 173233 and 371587 from that soource
> string, how can I do that?
> 

Use split() to split the string into four strings, using spaces as
separators, then use int() to convert the resulting strings that
interest you.

>>> a, b, c, d = 'Total size: 173233 (371857)'.split()
>>> first_int, second_int = int(c), int(d[1:-1])
>>> first_int
173233
>>> second_int
371857

HTH
Steve P



More information about the Python-list mailing list