python find numbers in string || Python find numbers in string ||Find numbers in a string python || Find numbers in string python
How to Find Numbers in String Using Python
One of the most popular programming languages, Python is hailed for its simplicity and flexibility. Extracting numbers from a string in Python is one of the most common tasks. It can be easily performed with the help of regular expressions. In this article, we will discuss different methods to find numbers in a text string effectively using Python. Here are the approaches
Extracting Numbers with Regular Expressions
Regular expressions (regex) in Python are powerful tools for matching different text strings or patterns. This is done using Pythons re-module, which helps us search for and find numbers inside a string.
import re
text = "The price is 250 dollars, and the discount is 15%"
numbers = re.findall(r'\d+', text)
print(numbers)
This method scans the string to collect all integers and stores them inside a list. The output of the above example would be [‘250′, ’15’]
Extracting Numbers Through a For Loop
Alternatively, you can use a for loop to iterate over each character in the string if you prefer simplicity and only want numbers.
text = "There are 3 apples and 24 bananas."
numbers = [char for char in text if char.isdigit()]
print(numbers)
Since we are looking for single digits, this method would return [3, 2,4]. I am capturing digits more significant than one, as 24 is not combined. Okay to do that you can modify the code a little by grouping them together for example.
A More Concise Solution via List Comprehension
List comprehension is a compact way of getting numbers from a string. Here’s an example:
text = "She bought 5 items for a total of 120 dollars."
numbers = ''.join([char if char.isdigit() else ' ' for char in text]).split()
print(numbers)
This converts all non-digit characters to spaces, then splits and returns the numbers. Output: [‘5’, ‘120’]
Switching strings to integers and doubles
Along with fetching the numbers, you will maybe need to convert them into integers or floating points for more operations. Here’s how to do it:
text = "Her score was 98.5, and his score was 87."
numbers = re.findall(r'\d+\.?\d*', text)
float_numbers = [float(num) for num in numbers]
print(float_numbers)
For this example, the result should be [98.5,87.0] We can easily capture integers as well as decimal numbers using this approach.
Using the split()
Method
A simple alternative way to getting the numbers is using split() function. This splits the string by words or characters, you could remove not a number in this part.
text = "The race had 200 participants and lasted 3 hours."
words = text.split()
numbers = [word for word in words if word.isdigit()]
print(numbers)
This would give [‘200’, ‘3’], splitting the string at words but taking only those that are numbers.
Conclusion
In Python, we have multiple ways of extracting numbers from strings, and depending on the use case you should choose one. Applying regular expressions, looping, or using list comprehensions are all tasks that can be carried out much more simply with Python.
1 thought on “Python Find Numbers in string”