How to Extract Numbers from a String in Python Using Regular Expressions


In this article, we show how to extract numbers from a string in Python using regular expressions.

So, say, we have the string, "I have 3 kids"

In certain cases in code, we may want to extract just the number from the string.

In Python, with regular expressions, we can do this easily.

We simply write a regular expression that matches digits. Any digits matched in the string will be returned as output.

The regular expression statement that matches digits is shown below.

This regular expression above will match all digits found in the string and return only the digits (and not the non-digit characters).

To get the full picture, let's look at a complete example.

This is shown in the code below.

So the first thing is that in order to use regular expressions in Python, you have to import the re module. So this is the first thing we do in our code above.

Next, we have the phrase that we extract from, "I have 3 kids"

Our goal is to write a regular expression that gets the amount of kids that the person has from the string.

We then have our regular expression which we assign to the variable, patterns.

Next, when you're using a regular expression to match a pattern in a string, you must use a for loop for the pattern that you create.

The reason for this is that patterns checks multiple instances of the string. Therefore, it is not just checking the entire string, "I have 3 kids" just all as a whole. It is checking every instance of the string to see if there are multiple areas in the string that matches this pattern. This is more easily understandable when you have multiple numbers in a string. In the case of having multiple numbers in the string, it will return each number separately rather than as a clump, which is what is more than likely intended.

We then create a variable called match and set it equal to, re.findall(p, phrase)

With this line, we are looking to see if any of the phrase has digits.

If so, Python returns it as a list.

We then print out the result.

The result we get is shown below.

We can also create the code with a function, so that we can just call the function and return the results.

This is shown below.

The code above returns the same value of 3.

Multiple Numbers in a String

Lastly, let's look at an example, where there are multiple numbers in a string.

Let's look at what Python returns when we extract the numbers from a string with multiple numbers.

This is the same exact code as above, but just a different string that contains multiple numbers, this time.

The code, above, returns the following output.

So you see that Python just returns the numbers as a list.

So this is extracting numbers (or digits) from a string in Python using regular expressions.

Related Resources

How to Randomly Select From or Shuffle a List in Python

HTML Comment Box is loading comments...