In this Python tutorial, we will learn how to find a number in the string by using Python.
Now, in Python, we can extract or find number in Python String using multiple ways. For example, we can use isdigit(), re.findall(), split(), etc to find number in Python String.
These are 4 ways to find number in a string in Python.
- Using regex module
- Using isdigit()
- Using split() and append()
- Using Numbers from String library
Python find number in string 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)
- In the above code, we have defined a string and assigned integers and alphabetic characters to it.
- Next, we used the for loop to iterate over each character defined in the string.
- And used the isdigit() method with IF condition to determine which character is a number or digit.
Here is the execution of the following given code.
Read: Remove a character from a Python string through index
Python Find number in string 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)
- 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 you will print the ’emp_lis’ list then the output will display only a list that contains an integer value.
Read: Python string formatting
Python Find number in 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 which 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)
- 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.
Read: Python string to list
Python Find number in 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.
# Importing the package
import nums_from_string
# Defining 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.'''
# Printing list of numbers
print("List of numbers from sample_string are: ")
print(nums_from_string.get_nums(sample_string))
- 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.
You may like the following Python tutorials:
- Python program to reverse a string
- Python string formatting
- Python compare strings
- Python split string by space
Conclusion
So, in this Python tutorial, we have understood how to find numbers in string in Python using multiple methods. Also, we illustrated each method using an example in Python.
Here is the list of methods.
- Extract number in Python string using regex module
- Check number in Python string using isdigit()
- Detect number in Python string using split() and append()
- Python Find number in string using Numbers from String library
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.