Python find number in String

Python find number in String

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:

Output::

12 out of 15 boys and 8 out of 12 girls are present
[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.

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:

Output:

12 out of 15 boys and 8 out of 12 girls are present
[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

Output:

Input String: 1a5c439a
[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

Output:

Input String: 1a5c439a
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:

Output::

[12, 15, 8, 12]

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:

After installing the library, the following is the code that will get you your desired result:

We use the get_nums() function of the nums_from_string library to get number from string in Python.

Output::

[12, 15, 8, 12]

That’s all about Python find number in String.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *