67

Suppose I had a string

string1 = "498results should get" 

Now I need to get only integer values from the string like 498. Here I don't want to use list slicing because the integer values may increase like these examples:

string2 = "49867results should get" 
string3 = "497543results should get" 

So I want to get only integer values out from the string exactly in the same order. I mean like 498,49867,497543 from string1,string2,string3 respectively.

Can anyone let me know how to do this in a one or two lines?

Bill the Lizard
  • 386,424
  • 207
  • 554
  • 861
Shiva Krishna Bavandla
  • 23,288
  • 68
  • 183
  • 305

10 Answers10

133
>>> import re
>>> string1 = "498results should get"
>>> int(re.search(r'\d+', string1).group())
498

If there are multiple integers in the string:

>>> map(int, re.findall(r'\d+', string1))
[498]
jamylak
  • 120,885
  • 29
  • 225
  • 225
  • 3
    this is not exactly correct, yes it will do for string **starting** with a integer, but if the integer is in the middle of the string, it won't do. Maybe it should be better to use `int(re.search(r'\d+', string1).group())` – eLRuLL Mar 20 '14 at 17:38
  • 1
    If `string1` is **(020) 3493**, it fails. – Hussain Mar 19 '15 at 12:53
  • Right it was the wrong code, should ahve used `re.search` – jamylak Mar 19 '15 at 14:40
  • 1
    @Hussain well, the example only showed numbers at the fron though but anyway now it's safe either way – jamylak Mar 19 '15 at 14:41
  • 2
    This will work fine. But, one should check if result of `re.search` is not **None**. Otherwise, it'll throw an **AttributeError** (`'NoneType' object has no attribute 'group'`) if there is no digit present in the string after accessing `.group()`. – Mohsin Jun 15 '17 at 10:14
  • @gofmtyourself Easier to ask for forgiveness than permission. The error can be handled in `try` `except AttributeError` – jamylak Jun 19 '17 at 22:05
55

An answer taken from ChristopheD here: https://stackoverflow.com/a/2500023/1225603

r = "456results string789"
s = ''.join(x for x in r if x.isdigit())
print int(s)
456789
Community
  • 1
  • 1
Sepero
  • 4,055
  • 1
  • 26
  • 22
24

Here's your one-liner, without using any regular expressions, which can get expensive at times:

>>> ''.join(filter(str.isdigit, "1234GAgade5312djdl0"))

returns:

'123453120'
Bobort
  • 2,847
  • 31
  • 39
19

if you have multiple sets of numbers then this is another option

>>> import re
>>> print(re.findall('\d+', 'xyz123abc456def789'))
['123', '456', '789']

its no good for floating point number strings though.

jacanterbury
  • 1,305
  • 1
  • 23
  • 32
10

Iterator version

>>> import re
>>> string1 = "498results should get"
>>> [int(x.group()) for x in re.finditer(r'\d+', string1)]
[498]
John La Rooy
  • 281,034
  • 50
  • 354
  • 495
7
>>> import itertools
>>> int(''.join(itertools.takewhile(lambda s: s.isdigit(), string1)))
Craig Citro
  • 6,327
  • 1
  • 29
  • 28
  • 1
    This will only work if the number is at the start of the string. Also, why not use `str.isdigit` instead of the lambda? – John La Rooy Jul 05 '12 at 07:15
  • 2
    This could also be written as `int(''.join(itertools.takewhile(str.isdigit, string1)))`. I would never actually use either method since this is overcomplicating it. – jamylak Jul 05 '12 at 07:17
  • https://groups.google.com/forum/?hl=en&fromgroups#!msg/alt.religion.emacs/DR057Srw5-c/Co-2L2BKn7UJ – Craig Citro Jul 05 '12 at 07:23
  • 1
    I'm planning ahead for the case where the strings are thousands of characters long. – Craig Citro Jul 05 '12 at 07:46
3

With python 3.6, these two lines return a list (may be empty)

>>[int(x) for x in re.findall('\d+', your_string)]

Similar to

>>list(map(int, re.findall('\d+', your_string))
huseyin39
  • 1,293
  • 11
  • 17
0
def function(string):  
    final = ''  
    for i in string:  
        try:   
            final += str(int(i))   
        except ValueError:  
            return int(final)  
print(function("4983results should get"))  
JochenJung
  • 7,083
  • 12
  • 64
  • 108
0

this approach uses list comprehension, just pass the string as argument to the function and it will return a list of integers in that string.

def getIntegers(string):
        numbers = [int(x) for x in string.split() if x.isnumeric()]
        return numbers

Like this

print(getIntegers('this text contains some numbers like 3 5 and 7'))

Output

[3, 5, 7]
0

Another option is to remove the trailing the letters using rstrip and string.ascii_lowercase (to get the letters):

import string
out = [int(s.replace(' ','').rstrip(string.ascii_lowercase)) for s in strings]

Output:

[498, 49867, 497543]