Hello all,
I'll start by saying please bear with me, I am very new to python and I need some help takeing a string with mixed letters and numbers and converting it to a INT. For example

``stringname = '105 mV'``

I would like '105 'striped from the string. of course if you cast like:

``intname = int(stringname)``

you get an error (ValueError: invalid literal for int() with base 10: '105 mV')

So my question is, is there a method that will do this for me. Currently I do this:

``intname = int(stringname.replace(' mV',''))``

and is seems to work but somehow seems a little clumsy.

any suggestions???

Also if anyone knows of a good resource for sting methods I would very much appreciate you sharing with me.

Thanks

Answered by bvdet 75 in a post from

A general solution:

``````def str_to_int(s):
return int(''.join([c for c in s if c in '1234567890']))

s = '105 mV'
print str_to_int(s)``````

## All 4 Replies

A general solution:

``````def str_to_int(s):
return int(''.join([c for c in s if c in '1234567890']))

s = '105 mV'
print str_to_int(s)``````

Alternately if you knew there would always be a space after the number:

``````s = '105 mV'
number = s.split(' ')[0]
print number``````

thanks for you input, I will give these example a try.

You could also use `c.isdigit()` instead of `c in '123456789'` for the first example.