Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse string to int when string contains a number + extra characters [closed]

Tags:

python

Using Python, how can/should I parse a string that has a number, followed by other characters, to an int? The specific problem I am trying to solve is parsing the first number out of a string containing a number followed by an arbitrary amount of other characters, including, possibly other numbers, which I am not interested in.

For example, if the string is "12//1" I need to get just the 12 to an integer.

like image 901
B Robster Avatar asked Jul 26 '13 18:07

B Robster


1 Answers

If you want to extract the digits in the string:

int(''.join(c for c in s if c.isdigit()))
like image 113
jh314 Avatar answered Sep 22 '22 03:09

jh314