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.

Related Posts

  • Python hex to bytes
    31 May

    Convert Hex to bytes in Python

    Table of ContentsUse codecs.decode() function to Convert Hexadecimal value to Byte in PythonUse bytes.fromhex() function to Convert Hexadecimal value to Byte in PythonUse the binascii Module to Convert Hexadecimal value to Byte in Python 💡 Outline You can use codecs’s decode() function to convert Hex to bytes in python. This method works for Python 2.x […]

  • Remove commas from String in Python
    23 May

    Remove commas from String in Python

    Table of ContentsUsing replace() function to remove commas from string in PythonUsing re or RegEx package function to remove commas from string in Python 💡 Outline You can remove commas from string in python by using string’sreplace() method. Syntax: [crayon-60cbc4ea64164822045460/] In this tutorial, we will take a sample string with a couple of commas in […]

  • Add character to String in Python
    21 May

    Add character to String in Python

    Table of ContentsUsing the + operatorUsing the join() functionUsing the % operatorUsing the format() functionUsing the f-strings Strings are considered as a sequence of characters in programming. In Python, string objects are considered immutable. What this means is that string objects cannot be changed once created, and a new string is created with a different […]

  • 13 April

    How to Format Float to 2 Decimal Places in Python?

    In this article you will learn - How to Format a Float With Two Decimal Places in Python?

  • 11 April

    Python Remove Newline from List

    Table of ContentsIntroduction✨ Method 1: Using the replace() method✨ Method 2: Using re.sub()✨ Method 3: Using strip()✨ Method 4: Slice the List Elements✨ Method 5: Using List Comprehension and Splitlines Function✨ Method 6: Using str.maketrans(x,y) and str.translate(map)Conclusion 🎯 Objective: In this article, we will discuss the methods to remove ‘\n’ or newline characters from a […]

  • 03 April

    Sort String Alphabetically in Python

    In this article you will learn:- How to sort a string alphabetically in Python?

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.