In this tutorial, we will look at how to extract numbers from a string in Python with the help of examples.
How to extract numbers from string?
You can iterate through the string and use the string isdigit()
function to extract numbers from a string in Python. Use the following steps –
- Intialize our resulting string to an empty string.
- Iterate through the string.
- For each character in the string, use the
isdigit()
function to check if its a digit or not. - 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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
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.
Using regular expressions to extract numbers from string
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.