One of the common tasks I faced while working with Python was extracting and counting numbers hidden inside a string.
At first glance, it sounds simple. But when you actually try to count numbers in a string, you quickly realize there are multiple approaches in Python, each with its own benefits.
In this tutorial, I’ll walk you through several practical methods I use to count numbers in strings using Python. I’ll explain each method with real code examples, so you can copy, run, and adapt them to your own projects.
Methods to Count Numbers in a String Using Python
In real‑world applications, strings often contain a mix of letters, numbers, and special characters. For example, think about:
- Extracting invoice numbers from customer emails.
- Counting digits in product codes.
- Parsing log files that mix text with numeric IDs.
Python makes these tasks surprisingly easy once you know the right tools.
Method 1 – Use Python’s isnumeric() Method
The first method I often use is Python’s built‑in isnumeric() method. This method checks if a character is numeric.
Here’s a simple example:
text = "Invoice 245 was paid on 09/15/2025 in New York."
count = 0
for char in text:
if char.isnumeric():
count += 1
print("Total numbers in the string:", count)You can refer to the screenshot below to see the output.

In this code, I loop through each character in the string. This method is easy, but it counts digits individually. For example, “245” will be counted as three numbers.
Method 2 – Use Python’s isdigit() Method
Another approach is to use the isdigit() method, which works similarly to the isnumeric() method in Python.
Here’s how I use it:
text = "My order number is 45789 and it was shipped on 09/12/2025."
count = sum(1 for char in text if char.isdigit())
print("Total numbers in the string:", count)You can refer to the screenshot below to see the output.

This code uses a generator expression with sum(), making it more compact. Just like isnumeric(), it counts each digit separately. So “45789” will be counted as 5 digits.
Method 3 – Use Regular Expressions (Regex)
If I want to count whole numbers (like 45789 as one number instead of five digits), I turn to Python’s re module.
Here’s how:
import re
text = "Customer ID 12345 has 2 pending orders and 15 completed ones."
numbers = re.findall(r'\d+', text)
count = len(numbers)
print("Numbers found:", numbers)
print("Total numbers in the string:", count)You can refer to the screenshot below to see the output.

With \d+, the regex finds sequences of digits. In this example, it will return [‘12345’, ‘2’, ’15’]. This method is very useful when you need to work with complete numbers rather than individual digits.
Method 4 – Use Python List Comprehension
List comprehensions are one of my favorite Python features. They make the code both concise and readable.
Here’s how I use them to count digits:
text = "Flight AA123 departs at 07:45 and arrives at 10:30."
digits = [char for char in text if char.isdigit()]
print("Digits found:", digits)
print("Total digits in the string:", len(digits))You can refer to the screenshot below to see the output.

This method is great when you want to quickly see which digits are present in the string.
Method 5 – Use Python’s Counter from Collections
Sometimes I want not just the count but also the frequency of each digit. Python’s collections.Counter is perfect for this.
from collections import Counter
text = "Zip codes: 10001, 90210, and 33101 are popular in the USA."
digit_counter = Counter(char for char in text if char.isdigit())
print("Digit frequencies:", digit_counter)
print("Total digits in the string:", sum(digit_counter.values()))You can refer to the screenshot below to see the output.

This method tells me how many times each digit appears. For example, in the zip codes above, digits like 0 and 1 occur more often.
Method 6 – Split and Check with Python
Another trick I use is splitting the string into words and checking each word. This helps when the numbers are separated by spaces.
text = "The package weights are 15 20 35 and 50 pounds."
count = 0
for word in text.split():
if word.isdigit():
count += 1
print("Total whole numbers in the string:", count)This method is simple and works well when numbers are clearly separated by spaces.
Method 7 – Combine Multiple Approaches
In real projects, I often combine methods. For example, I might first use regex to extract numbers, then use Python list comprehension to process them further.
import re
text = "On 09/15/2025, order 1234 was shipped with tracking number 56789."
numbers = re.findall(r'\d+', text)
lengths = [len(num) for num in numbers]
print("Numbers found:", numbers)
print("Length of each number:", lengths)
print("Total numbers in the string:", len(numbers))This way, I not only count the numbers but also analyze their length, which can be useful for validating IDs or codes.
Which Python Method Should You Use?
Let me show you which method you need to use in Python based on requirement.
- Use isnumeric() or isdigit() when you want to count individual digits.
- Use Regex (re) when you want to count whole numbers.
- Use List Comprehension for quick and clean code.
- Use Counter when you need frequency analysis.
- Use splitting when numbers are separated by spaces.
Each method has its place, and as a Python developer, I choose the one that best fits my specific problem.
When I look back at my early projects, I realize how much easier my work became once I mastered these simple techniques.
Now, whenever I need to parse invoices, logs, or data files, I can quickly count numbers in strings using Python in just a few lines of code.
I hope you found this tutorial helpful. Try out these methods in your own projects, and you’ll see how flexible and powerful Python really is when it comes to text processing.
You may also like to read other Python articles:
- Flatten a List of Lists in Python
- Sort a List of Tuples in Python
- Get the Last Element of a List in Python
- Split a List in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.