Skip to Content

Extract Numbers From String in Python

In this tutorial, we will look at how to extract numbers from a string in Python with the help of examples.

Extract numbers from string in python

You can iterate through the string and use the string isdigit() function to extract numbers from a string in Python. Use the following steps –

  1. Intialize our resulting string to an empty string.
  2. Iterate through the string.
  3. For each character in the string, use the isdigit() function to check if its a digit or not.
  4. If it is a digit, add the character to our result string.

Let’s look at an example to see the above steps in action in Python.

# string with numbers
s = "I love you 3000"
# result string 
s_nums = ""
# iterate over characters in s
for ch in s:
    if ch.isdigit():
        s_nums += ch
# display the result string
print(s_nums)

Output:

3000

The resulting string contains only digits from the original string.

You can reduce the above code to a single line by using a list comprehension and the string join() function.

# string with numbers
s = "I love you 3000"
# result string 
s_nums = "".join([ch for ch in s if ch.isdigit()])
# display the result string
print(s_nums)

Output:

3000

We get the same result as above. The resulting string only contains numerical characters. Here, we use a list comprehension to get a list numerical characters in the string. We then use the string join() function to join these characters back to a string.

Note that this method will only capture the digits in the string. For example, if the string contains negative numbers, this method will only capture the digits and not the sign.

# string with numbers
s = "The temperature is -5 degrees celsius."
# result string 
s_nums = "".join([ch for ch in s if ch.isdigit()])
# display the result string
print(s_nums)

Output:

5

Here, we get the digit in the string in our resulting string but we don’t get the sign for the negative number.

Alternatively, you can use regular expressions to extract numbers from a string. Let’s use it to capture the digits in a string.

import re

# string with numbers
s = "I love you 3000"
# result list 
s_nums = re.findall(r'\d', s)
# display the result list
print(s_nums)
print("".join(s_nums))

Output:

['3', '0', '0', '0']
3000

We get the numbers from the string in our result list. You can also capture contiguous digits using the r'\d+' regular expression.

A good thing about using regular expressions is that you can customize them to capture patterns relevant to you. For example, if you also want to capture negative integers (with sign) you can use the regular expression, r'-?\d+'

Let’s look at an example.

import re

# string with numbers
s = "The temperature on the 27th of December was -5 degrees celsius."
# result list 
s_nums = re.findall(r'-?\d+', s)
# display the result list
print(s_nums)

Output:

['27', '-5']

We get the numbers in the string along with the negative sign (if present) in our result list.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.