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.


import_contacts

You may also like:

Related Posts

  • Remove Urls from String in Python
    03 May

    Remove Urls from Text in Python

    Table of ContentsIntroductionWays to remove URLs from Text in PythonUsing the re.sub() function to remove URLs from Text in PythonUsing the re.findall() function to remove URLs from Text in PythonUsing the re.search() function to remove URLs from Text in PythonUsing the urllib.urlparse class to remove URLs from Text in PythonConclusion In this post, we will […]

  • 16 February

    Remove Backslash from String in Python

    Table of ContentsHow to remove backslash from string in python?Using replace() Function to Remove Backslash from String in PythonUsing the decode() Function to Remove Backslash from String in PythonUsing re Library Functions to Remove Backslash from String in PythonUsing strip() Function to Remove Backslash from String in PythonHow To Remove Backslash from Json String in […]

  • Remove quotation marks from String in Python
    13 February

    Remove Quotation Marks from String in Python

    Table of ContentsQuotes in PythonRemove Double Quotes from String in PythonUsing the replace() Function to Remove Double Quotes from String in PythonUsing the re.sub() Function to Remove Double Quotes from String in PythonUsing the for Loop to Remove Double Quotes from String in PythonUsing the join() Function to Remove Double Quotes from String in PythonUsing […]

  • Parentheses from String in Python
    08 February

    Remove Parentheses from String in Python

    Table of ContentsWays to Remove Parentheses from String in PythonUsing the replace() Function to Remove Parentheses from String in PythonUsing the re.Sub() Function to Remove Parentheses from String in PythonUsing the pandas.Str.Replace() Function to Remove Parentheses from String in PythonConclusion In this article, we will see how to remove parentheses from String in Python. Parentheses […]

  • Remove word from String in Python
    08 February

    Remove Word from String in Python

    Table of ContentsRemove Word from String in PythonHow to Remove Word from Sentence in PythonUsing the replace() functionUsing the re.sub() functionUsing the startswith() functionUsing the removeprefix() functionUsing the endswith() functionUsing the removesuffix() functionHow to Remove Duplicate Words from String in PythonUsing the set() functionUsing the set() and join() functionsUsing the join() and a user-defined functionUsing […]

  • 22 January

    Replace space with underscore in Python

    Table of ContentsWays to replace space with underscore in PythonUsing the for loopUsing the replace() functionUsing the re.sub() functionUsing the split() and join() functionConclusion Strings are an essential data type in programming. In Python, we can treat strings as an iterable of characters, and can perform a variety of functions and operations on them. Replacing […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.