[python] Extract Number from String in Python

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.

This question is related to python string python-3.x

The answer is


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

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

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

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.)


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']

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

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

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

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

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

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

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]

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

int(str1.split()[0])

would do it


#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)

Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to string

How to split a string in two and store it in a field String method cannot be found in a main class method Kotlin - How to correctly concatenate a String Replacing a character from a certain index Remove quotes from String in Python Detect whether a Python string is a number or a letter How does String substring work in Swift How does String.Index work in Swift swift 3.0 Data to String? How to parse JSON string in Typescript

Examples related to python-3.x

Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation Replace specific text with a redacted version using Python Upgrade to python 3.8 using conda "Permission Denied" trying to run Python on Windows 10 Python: 'ModuleNotFoundError' when trying to import module from imported package What is the meaning of "Failed building wheel for X" in pip install? How to downgrade python from 3.7 to 3.6 I can't install pyaudio on Windows? How to solve "error: Microsoft Visual C++ 14.0 is required."? Iterating over arrays in Python 3 How to upgrade Python version to 3.7?