Extract Number from String in Python

PythonStringPython 3.x

Python Problem Overview


I am new to Python and I have a String, I want to extract the numbers from the string. For example:

str1 = "3158 reviews"
print (re.findall('\d+', str1 ))

Output is ['4', '3']

I want to get 3158 only, as an Integer preferably, not as List.

Python Solutions


Solution 1 - Python

You can filter the string by digits using str.isdigit method,

>>> int(filter(str.isdigit, str1))
3158

Solution 2 - Python

This code works fine. There is definitely some other problem:

>>> str1 = "3158 reviews"
>>> print (re.findall('\d+', str1 ))
['3158']

Solution 3 - Python

Your regex looks correct. Are you sure you haven't made a mistake with the variable names? In your code above you mixup total_hotel_reviews_string and str.

>>> import re
>>> s = "3158 reviews"
>>> 
>>> print(re.findall("\d+", s))
['3158']

Solution 4 - Python

IntVar = int("".join(filter(str.isdigit, StringVar)))

Solution 5 - Python

You were quite close to the final answer. Your re.finadall expression was only missing the enclosing parenthesis to catch all detected numbers:

> re.findall( '(\d+)', str1 )

For a more general string like str1 = "3158 reviews, 432 users", this code would yield:

> Output: ['3158', '432']

Now to obtain integers, you can map the int function to convert strings into integers:

> A = list(map(int,re.findall('(\d+)',str1)))

Alternatively, you can use this one-liner loop:

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

Both methods are equally correct. They yield A = [3158, 432].

Your final result for the original question would be first entry in the array A, so we arrive at any of these expressions:

> result = list(map(int,re.findall( '(\d+)' , str1 )))[0]

> result = int(re.findall( '(\d+)' , str1 )[0])

Even if there is only one number present in str1, re.findall will still return a list, so you need to retrieve the first element A[0] manually.

Solution 6 - Python

To extract a single number from a string you can use re.search(), which returns the first match (or None):

>>> import re
>>> string = '3158 reviews'
>>> int(re.search(r'\d+', string).group(0))
3158

In Python 3.6+ you can also index into a match object instead of using group():

>>> int(re.search(r'\d+', string)[0])
3158

Solution 7 - Python

Best for every complex types

str1 = "sg-23.0 300sdf343fc  -34rrf-3.4r" #All kinds of occurrence of numbers between strings
num = [float(s) for s in re.findall(r'-?\d+\.?\d*', str1)]
print(num)

Output:

[-23.0, 300.0, 343.0, -34.0, -3.4]

Solution 8 - Python

If the format is that simple (a space separates the number from the rest) then

int(str1.split()[0])

would do it

Solution 9 - Python

There may be a little problem with code from Vishnu's answer. If there is no digits in the string it will return ValueError. Here is my suggestion avoid this:

>>> digit = lambda x: int(filter(str.isdigit, x) or 0)
>>> digit('3158 reviews')
3158
>>> digit('reviews')
0

Solution 10 - Python

Above solutions seem to assume integers. Here's a minor modification to allow decimals:

num = float("".join(filter(lambda d: str.isdigit(d) or d == '.', inputString)

(Doesn't account for - sign, and assumes any period is properly placed in digit string, not just some english-language period lying around. It's not built to be indestructible, but worked for my data case.)

Solution 11 - Python

My answer does not require any additional libraries, and it's easy to understand. But you have to notice that if there's more than one number inside a string, my code will concatenate them together.

def search_number_string(string):
    index_list = []
    del index_list[:]
    for i, x in enumerate(string):
        if x.isdigit() == True:
            index_list.append(i)
    start = index_list[0]
    end = index_list[-1] + 1
    number = string[start:end]
    return number

Solution 12 - Python

a = []
line = "abcd 3455 ijkl 56.78 ij"
for word in line.split():
 try:
  a.append(float(word))
  except ValueError:
  pass
print(a)

OUTPUT

3455.0 56.78


  

  

Solution 13 - Python

I am a beginner in coding. This is my attempt to answer the questions. Used Python3.7 version without importing any libraries.

This code extracts and returns a decimal number from a string made of sets of characters separated by blanks (words).

Attention: In case there are more than one number, it returns the last value.

line = input ('Please enter your string ')
for word in line.split():
    try:
        a=float(word)
        print (a)
    except ValueError:
        pass

Solution 14 - Python

For python3

input_str = '21ddd3322'
int(''.join(filter(str.isdigit, input_str)))

> 213322

Solution 15 - Python

#Use this, THIS IS FOR EXTRACTING NUMBER FROM STRING IN GENERAL. #To get all the numeric occurences.

*split function to convert string to list and then the list comprehension which can help us iterating through the list and is digit function helps to get the digit out of a string.

getting number from string

use list comprehension+isdigit()

test_string = "i have four ballons for 2 kids"

print("The original string : "+ test_string)


# list comprehension + isdigit() +split()

res = [int(i) for i in test_string.split() if i.isdigit()]
print("The numbers list is : "+ str(res))

#To extract numeric values from a string in python

*Find list of all integer numbers in string separated by lower case characters using re.findall(expression,string) method.

*Convert each number in form of string into decimal number and then find max of it.

import re 
 def extractMax(input):

# get a list of all numbers separated by lower case characters
 
 numbers = re.findall('\d+',input) 

# \d+ is a regular expression which means one or more digit

number = map(int,numbers)
print max(numbers)
if __name__=="__main__":
input = 'sting'
extractMax(input)

Solution 16 - Python

you can use the below method to extract all numbers from a string.

def extract_numbers_from_string(string):
    number = ''
    for i in string:
        try:
            number += str(int(i))
        except:
            pass
    return number

(OR) you could use i.isdigit() or i.isnumeric(in Python 3.6.5 or above)

def extract_numbers_from_string(string):
    number = ''
    for i in string:
        if i.isnumeric():
            number += str(int(i))
    return number


a = '343fdfd3'
print (extract_numbers_from_string(a))
# 3433

Solution 17 - Python

For Python 2.7:

>>> str1 = '~ 44 million people'
>>> int(filter(str.isdigit, str1))
44

Cumbersome, but for Python 3:

>>> str1 = '~ 44 million people'
>>> int(''.join(filter(str.isdigit, str1)))
44

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUmair AyubView Question on Stackoverflow
Solution 1 - PythonVishnu UpadhyayView Answer on Stackoverflow
Solution 2 - PythonIrshad BhatView Answer on Stackoverflow
Solution 3 - PythonspaceghostView Answer on Stackoverflow
Solution 4 - PythonBernardView Answer on Stackoverflow
Solution 5 - PythonC-3POView Answer on Stackoverflow
Solution 6 - PythonEugene YarmashView Answer on Stackoverflow
Solution 7 - Pythonuser13959036View Answer on Stackoverflow
Solution 8 - Python6502View Answer on Stackoverflow
Solution 9 - PythonM.DanilchenkoView Answer on Stackoverflow
Solution 10 - PythonthundView Answer on Stackoverflow
Solution 11 - PythonYang QiView Answer on Stackoverflow
Solution 12 - PythonAyush SinghView Answer on Stackoverflow
Solution 13 - PythonKaouther Ghabri St. ClairView Answer on Stackoverflow
Solution 14 - PythonBinh HoView Answer on Stackoverflow
Solution 15 - PythonAyush SinghView Answer on Stackoverflow
Solution 16 - PythonSuperNovaView Answer on Stackoverflow
Solution 17 - PythondarkstarView Answer on Stackoverflow