Count numbers in string in Python [5 Methods]

In this Python tutorial, we will understand how to count numbers in string in Python using examples.

In Python, we can count numbers from a string using the following 5 methods:

  • Using isalpha() method
  • Using all digits list
  • Using isnumeric() method
  • Using re module
  • Using ord() function

Let us start with the isnumeric() method.

Count numbers in string in Python using isnumeric()

In this section, we will learn how to count numbers in string in Python using the isnumeric() method.

The Python isnumeric() is a string method that return TRUE only when the character defined in string is numeric. However, if the character is not numeric, it will return FALSE.

Let us understand how to use this method to fetch the count of numbers in string in Python.

# Defing a string with numbers
string = "Arizona29876"
 
# Initializing count
count = 0
 
# Iterating over all characters
for num in string:
 
    # Check character is digit (number)
    if num.isnumeric():
        # Increment the value of count
        count += 1

# Final count
print("Total numbers found:-", count)
  • In the example, we utilized the for loop to iterate over each character defined in the string.
  • After this, we used the isnumeric() method to check if the character is numeric or not. And based on this, we are calculating the count.

Here is the final result of the above Python program.

Count numbers in string in Python
Counting numbers in string in Python

So, here we seen how to count numbers in string in Python using isnumeric() method.

Also, read: Python format number with commas

Count numbers in string in Python using isalpha()

This section will illustrate how to use the isalpha() method to check the count of numbers from Python string.

The Python isalpha() method allows us to check if a character in a given string is alphabet or not (a-z). So, this method returns TRUE, if the character is alphabet, else it returns FALSE.

Let us understand its use in Pyhton with the help of an example.

# Defing a string with numbers
string = "Chicago09876545"
 
# Initializing count
alpha_count = 0
 
# Iterating over all characters
for char in string:
 
    # Check character is alphabet
    if char.isalpha():
        # Increment the value of count
        alpha_count += 1


# num_count
num_count = len(string)-alpha_count

# Final count
print("Total numbers found:-", num_count)
  • In the above Python code, we used the isalpha() method to first count the number of characters that are the alphabet in the given string.
  • Next, we calculated the count of numbers by subtracting the alphabet character count from the total length of the string.

Here is the end of the Python program where the string is Chicago09876545.

Count numbers in Python String
Counting the numbers from String in Python

So, here we seen how to count the numbers in string in Python using isalpha() method.

Read: Python string formatting

Count numbers in string in Python using all digit list

In this section, we will understand how to count numbers in Python string using a list.

So, in this approach, we will first create a list containing all digits in string format. And then, we will check the existence of digit from string using the list.

Here is the example explaining this approach in Python program

# Defing a string with numbers
string = "Seattle987654"

# Defining list of digits
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
 
# Initializing count
count = 0
 
# Iterating over all characters
for num in string:
 
    # Check character is digit (number) using list
    if num in numbers:
        # Increment the value of count
        count += 1

# Final count
print("Total numbers found:-", count)
  • In this example, we are using the numbers list to check the existence of a digit in the given string.
  • And based on the existence, we are calculating the count and printing the result.
Count numbers in Python String Example
Counting numbers in Python String usinh List

So, here we seen how to count the numbers in string in Python using Python List.

Read: Python split string by space

Count numbers in string in Python using re module

In this section, we will understand the use of re module in Python to calculate the count of numbers from a Python String.

In findall() method in re module examines the given string from left to right and then, it return all the matching patterns. And in this example, we will define a pattern to search only for numbers (digits) from the string.

The example related to this implementation is given below.

# Importing re module
import re

# Defing a string with numbers
string = "Washington76805"
 
# Counting digits using finall() and len()
count = len(re.findall('[0-9]', string))
 
# Final count
print("Total numbers found:-", count)
  • In the example, we utilized the findall() method to first extract all the numbers given in the string.
  • After this, we used the len() function to get the count of the matching numbers.

So, when we run the above Python program, we will get the count of numbers from the given string.

Count numbers in Python String using re
Counting numbers in Python String using re module

So, here we seen how to count the numbers in string in Python using re.finall() method.

Read: Python find number in String

Count numbers in string in Python using ord()

This section will illustrate how to use the ord() function in Python to get the count of numbers from a given string.

The basic use of the ord() function in Python is to get the Unicode for any given character. And in this method, we will use ord() function to get the unicode for the digits starting from 0 to 1.

Then we will use the IF statement to check if the unicode for each character in the string matches any of the unicode for digits. And based upon this logic, we will calculate the count of numbers (digits).

The example related to this execution in Python is given below.

# Defing a string with numbers
string = "324Boston123"
 
# Initializing count
count = 0
 
# Iterating over all characters
for num in string:
 
    # Check character is digit (number)
    if ord(num) in range(48,58):
        # Increment the value of count
        count += 1

# Final count

print("Total numbers found:-", count)

Once we execute the above Python program, we will get the following result.

Count numbers in Python String using ord
Counting numbers in Python String using ord() function

So, here we seen how to count the numbers in string in Python using ord() function.

Conclusion

In this Python tutorial, we understood to get the count of numbers in string in Python using 5 different methods. Moreover, to explain each method, we illustrated multiple examples.

You may also like to read the following Python tutorials.