[Tutor] string to integer

Brian van den Broek broek at cc.umanitoba.ca
Fri Jan 6 06:41:06 CET 2006


Alan Gauld said unto the world upon 05/01/06 04:16 PM:
>>Here is how it should work:
>>val(7) = 7
>>val(bbab7) = 7
>>val(aa7aa) = 7
>>val(   7) = 7  
> 
> 
> There is no direct equivalent for val() as shpwn above the 
> nearest is int() or float() but that will only work with the first
> and last examples. (It is a pretty arbitrary functionn  to convert 
> aa7aa to 7!)
> What does val do with a6b5c?
> 
> You could strip all chars out of the string first:
> 
> 
> nums = [ch for ch in aString if ch in string.digits]
> val = int(str(nums))

<snip>

I'd spell that

nums = [ch for ch in aString if ch.isdigit()]

Best,

Brian vdB


More information about the Tutor mailing list