Extracting int from string

Jack Diederich jack at performancedrivers.com
Mon Jan 13 18:47:33 EST 2003


On Mon, Jan 13, 2003 at 04:44:52PM -0600, John Hunter wrote:
> >>>>> "Spencer" == Spencer Ernest Doidge <spencer at efn.org> writes:
> 
>     Spencer> What would be the best way to extract the integer value
>     Spencer> 45 from the following string?  'X= 45 A'
> 
> With no error checking, etc, the following should speed you on your
> way
> 
>   s = 'X=   45   A'
>   x = int(s.split()[1])
>   print x, type(x)

If you wanted to be a dick...

x = 45 # very efficient, correct for all specified cases

-jackdied






More information about the Python-list mailing list