Table of Contents
Extracting specific letters, phrases, words, and some specific characters is a very generic thing to do in the world of programming. In all these tasks, extraction of numbers and their occurrences from a string or a set of a string is also carried out very often by the user. Users can carry this task in many ways.
In this tutorial, we will see different ways by which we can find number in String in Python.
Use of re.findall()
function
This function comes under the RegEx module, an in-built package in python which deals with Regular Expressions. re
module is loaded in to use this package.
re.findall()
function helps in returning all the occurrences of an iteration or a pattern in the given string. A list object is returned in this function.
Example:
1 2 3 4 5 6 7 8 |
import re initial_string = "12 out of 15 boys and 8 out of 12 girls are present" print(initial_string) count = re.findall(r'\d+', initial_string) final_count = list(map(int, count)) print(str(final_count)) |
Output::
[12, 15, 8, 12]
In this method, we start by initializing a string from which we have to extract the numbers. Note that \d+
is being used in the above code which is used to match any character which is a decimal or an integer. Finally, we use the map()
function which returns an iterable function such as a list from any given function.
Further reading:
Use of List Comprehension, split()
and isdigit()
functions
As you can see, three functions are used in this method. List comprehension helps in creating a list based on values stored in an already existing list. In the Split()
function, a list is returned from an existing string. And isdigit()
checks if all the characters are numbers. If they are numbers, True
is returned and if they are not, False
is returned.
Example:
1 2 3 4 5 6 |
initial_string = "12 out of 15 boys and 8 out of 12 girls are present" print(initial_string) number_count = [int(x) for x in initial_string.split() if x.isdigit()] print(str(number_count)) |
Output:
[12, 15, 8, 12]
Here, spilt()
function converts the initial string into a list and then we check if there are numbers using isdigit()
function. Finally, all the numbers which are present in the initial string are returned in the form of a list.
If you want to search for number in String without space, then you can ignore split function in above code.
Here is an example
1 2 3 4 5 6 |
initial_string = "1a5c439a" print("Input String: ",initial_string) number_count = [int(x) for x in initial_string if x.isdigit()] print(str(number_count)) |
Output:
[1, 5, 4, 3, 9]
If you want to concatenate all numbers and create big number, then you can use join()
method of string.
Here is an example
1 2 3 4 5 6 7 8 9 10 |
initial_string = "1a5c439a" print("Input String: ",initial_string) number_count = [int(x) for x in initial_string if x.isdigit()] strNumber = [str(i) for i in number_count] # Join list items using join() resultNumber = int("".join(strNumber)) print(resultNumber) |
Output:
15439
Use of split()
and append()
functions
The append()
function is an in-built python function that is used to add a single value or an item to an already existing list. Basically, it modifies the existing list rather than making a new one. So a modified version of the existing list is returned by this function. This function also increases the size of that list.
Example:
1 2 3 4 5 6 7 8 9 10 |
initial_string = "12 out of 15 boys and 8 out of 12 girls are present" i = [] for s in initial_string.split(): try: i.append(int(s)) except ValueError: pass print(i) |
Output::
Here, we initially make an empty list and then iterate the whole string using a for loop after converting it into a list using the split()
method. Finally, we print the list after all the numbers are added to it.
Use of Numbers from String Library
Numbers from String Library is not a built-in library in python. This library is a shortcut and the easiest method for the user to find number in String in Python.
The library can be installed in python using the following command:
1 2 3 |
pip install nums_from_string |
After installing the library, the following is the code that will get you your desired result:
1 2 3 4 5 |
import nums_from_string initial_string = "12 out of 15 boys and 8 out of 12 girls are present" print(nums_from_string.get_nums(initial_string)) |
We use the get_nums()
function of the nums_from_string
library to get number from string in Python.
Output::
That’s all about Python find number in String.