Extracting int from string

Hans Nowak wurmy at earthlink.net
Mon Jan 13 23:28:52 CET 2003

Spencer Ernest Doidge wrote:
> What would be the best way to extract the integer value 45 from the following string?
> 'X=   45   A'

For this string:

 >>> s = 'X=   45   A'
 >>> parts = s.split()
 >>> parts
['X=', '45', 'A']
 >>> int(parts[1])

But if you have other strings where whitespace is used differently, e.g. 'X = 
45 A', then you might be better off using a regular expression.


Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA=='))
# decode for email address ;-)
The Pythonic Quarter:: http://www.awaretek.com/nowak/
Kaa:: http://www.awaretek.com/nowak/kaa.html

More information about the Python-list mailing list