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?


import_contacts

You may also like:

Related Posts

  • 19 May

    Remove First and Last Character of String in Python

    Table of ContentsHow to Remove First and Last Character of string in python?Remove First and Last Character of string Using For LoopRemove First and Last Character of string Using SlicingRemove First and Last word of string in PythonConclusionWas this post helpful? We use strings in python to handle text data. For processing the data, we […]

  • 19 May

    Remove HTML Tags From String in Python

    Table of ContentsRemove HTML tags from string in python Using Regular ExpressionsRemove HTML tags from string in python Using the lxml ModuleRemove HTML tags from string in python Using the Beautifulsoup ModuleConclusionWas this post helpful? While collecting data, we often need to process texts with HTML tags. In this article, we will discuss different ways […]

  • 19 May

    Remove xa0 from String in Python

    Table of ContentsHow to remove xa0 from string in python?Using the decode() function to remove xa0 from string in python.Using the re library to remove xa0 from string in python.Using the normalize() function from the unicodedata library to remove xa0 from string in python.Using the get_text() function from the BeautifulSoup library to remove xa0 from […]

  • 18 May

    Convert bool to String in Python

    Table of ContentsUsing the str() function to convert bool to string in Python.Using string formatting with the help of the % operator to convert bool to string in Python.Using string formatting with the help of the format() function to convert bool to string in Python.Using f-strings to convert bool to string in Python.Was this post […]

  • 18 May

    Convert UUID to String in Python

    Table of ContentsUUID in PythonWays to convert UUID to string in PythonUsing the str() function to convert UUID to String in PythonUsing the hex attribute to convert UUID to String in PythonUsing the urn attribute to convert UUID to String in PythonConclusionWas this post helpful? In this post, we will see how to convert UUID […]

  • 18 May

    How to Replace Word in String in Python

    Table of ContentsUsing the replace() Function to replace word in string in PythonUsing the re.Sub() Function to replace word in string in PythonUsing the re.subn() function to replace word in string in PythonConclusionWas this post helpful? In this tutorial, we will to how to replace word in string in Python. Strings represent a sequence of […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.