Python find number in String [4 Methods]

In this Python tutorial, we will learn how Python find number in string using various method with some illustrative examples.

To find numbers in a string using Python, you can use several methods. One effective way is to employ the re module’s findall() function with a regular expression pattern to extract digits. Alternatively, you can iterate over the string, use isdigit() to identify numbers, and split() the string to isolate them, appending the results to a list. Additionally, the numbers function from the string library offers a more direct approach to identify numerical characters in a string.

Python find number in string methods

Now, in Python, we can extract or find a number in Python String using multiple ways:

  • Using regex module
  • Using isdigit()
  • Using split() and append()
  • Using Numbers from String library

Let’s see them one by one using some examples:

Method 1: find number in string Python using isdigit()

In this section, we will understand how to find number in String in Python using the isdigit() method.

In Python, the isdigit() method returns True if all the digit characters are there in the input string. Moreover, this method allows the extraction of digits from the string in Python. If no character is a digit in the given string then it will return False.

Here is the syntax of using the isdigit() method in Python.

string.isdigit()

Note: However, this method does not take any argument and it always returns boolean value either TRUE or FALSE.

Let’s take an example and check how to find a number in a string in Python.

new_string = "Germany26China47Australia88"
 
emp_str = ""
for m in new_string:
    if m.isdigit():
        emp_str = emp_str + m
print("Find numbers from string:",emp_str) 

Output:

  • We used the for loop to iterate over each character defined in the string.
  • And used the isdigit() method with the if condition to determine which character is a number or digit.

Here is the execution of the following given code:

Find numbers from string: 264788
python find number in string

This way Python find number in string using split() with append() function.

Method 2: how to find number in string Python using split() and append()

In this section, we will discuss another method where we will find number in Python string using split() and append() methods.

In Python, the append() function is used to add an element to the end of a list. While the split() function in Python is used to break the string into a list.

Source Code:

new_str = "Micheal 89 George 94"

emp_lis = []
for z in new_str.split():
   if z.isdigit():
      emp_lis.append(int(z))

print("Find number in string:",emp_lis)

Output:

  • In the above example, we have used a for loop to iterate each word given in the new_str variable.
  • After this, we used the isdigit() method to find the number or int datatype.
  • Then we use the str.append() method and pass int(z) with the word to convert it into an integer and store it in the emp_lis list.

Once we print the list, the output will display only a list that contains an integer value.

new_str = "Micheal 89 George 94"

emp_lis = []
for z in new_str.split():
   if z.isdigit():
      emp_lis.append(int(z))

print("Find number in string:",emp_lis)
find digit in string python

This way Python find number in string using isdigit() function.

Method 3: Python extract number from string using regex module

In this section, we will learn how to find number in Python String using the regex module.

  • The re module in Python is the regex module that helps us to work with regular expressions.
  • We can fetch all the numbers from the string by using the regular expression‘[0-9]+’ with re.findall() method. The [0-9] represents finding all the characters that match from 0 to 9 and the + symbol indicates continuous digit characters.
  • However, the re.findall() method in Python is used to match the pattern in the string from left to right and it will return in the form of a list of strings.

Here is the syntax of using the re.findall() method in Python.

re.findall
          (
           pattern,
           string,
           flags=0
          )

Example:

Let’s take an example and check how to find a number in a string by using regular expressions in Python.

import re

new_string = 'Rose67lilly78Jasmine228Tulip'
new_result = re.findall('[0-9]+', new_string)
print(new_result)

Output:

  • In the above code, we have created a string type variable named ‘new_string’. This variable holds some integer and alphabetic characters.
  • Next, we utilized the re.findall() method to find all integer values from the new_string variable.

Once we will print the ‘new_result’ variable which is our result, we will get only integer values in the list Python:

['67', '78', '228']
extract number from string python

This way Python find number in string using regex module functions like findall().

Method 4: Python get number from string using Numbers from String library

In this section, we will learn how to fetch or find numbers in Python String using the nums_from_string module.

The nums_from_string module consists of multiple functions that can help to fetch integers or numeric string tokens from a given string. However, to use this module, first, we need to install it as it is not a built-in module in Python.

Here is the command that we can use to install the nums_from_string module in Python.

pip install nums_from_string

Once this package is installed in our Python environment, we need to use the get_nums() method to get from all numbers from a given string.

An example of this implementation is given below.

import nums_from_string

sample_string = '''There are 30 employees in the company, 
					out of which 10 are in the IT department, 
                	5 people are there in Marketing, 
                	8 people are there in Sales, 
                	2 are there in Legal, 
                	and 5 in Training.'''

print("List of numbers from sample_string are: ")
print(nums_from_string.get_nums(sample_string))

Output:

  • In the above example, we utilized the nums_from_string.get_nums() method to fetch all the numbers from the sample_string variable.
  • Moreover, this method will return a list containing all the number values that are there in the sample_string.

Here is the final result of the above Python program:

List of numbers from sample_string are
[30, 10, 5, 8, 2, 5]
get number from string python

This way Python find number in string using string library functions.

Conclusion

So, in this Python tutorial, we have understood how Python find number in string using multiple methods like regex() module, isdigit(), split() with append() function, and numbers from string library. Also, we illustrated each method using an example in Python.

You may like the following Python tutorials: