How to Count Numbers in String Python

In this Python tutorial, I will demonstrate how to count numbers in string Python.

I had to analyze the customer review dataset, and for that, I needed to extract or count the numerical rating within the customer’s text review. I used the Python ‘isnumeric()’ function to extract and count the numerical data from the review.

In this tutorial, I have explained how to use the ‘isnumeric()’ function to count the numbers in a string and one more approach to counting the numbers in a string.

Count Numbers in String Python

Sometimes, the string contains numbers, and you may need that number; in Python, you can use several ways or methods to extract those numbers, but here, you want to count the numbers in the string.

Let’s start,

Python Count Numbers in String Using isnumeric() Method

The isnumeric() function determines the character in the string, whether it is numeric or not. If the character is numeric, it returns True; otherwise, it returns False.

The syntax is given below.

str.isnumeric()

Where calling is ‘isnumeric()’ function on the ‘str’ string that contains the numeric character.

Let me show you, with an example, how you can use this ‘isnumeric()’ method. Take a random number and wrap it using a single quote. Then, on this string, call the ‘isnumeric()’ function, as shown in the code below.

str = '3456'
print(str.isnumeric())
Checking If String is Numeric using the isnumeric() Method

When you call the ‘isnumeric()’ function on the str=’3456′, it returns True because the string contains the numerical value.

READ:  How to return multiple values from a Python list

So, you will use this function and check the characters of the string; if the character is numeric, then a counter variable is incremented by one. Thus, after checking all characters of the string, the counter variable will have the count of the number in a string.

For example, you have the street address of any USA house, as shown below.

street = '4568 Brown Bear Drive'

To count the number in this ‘street’ address, use the isnumeric() method, as shown in the code below.

counter = 0

for character in street:
  if character.isnumeric():
    counter+=1

print("The number of numeric value in the string is:", counter)
Python Count Numbers in String Using isnumeric() Method

Look at the output; the string ‘4568 Brown Bear Drive’ contains 4 numbers. Let’s understand the code part of how it counts the numbers in the given string.

The ‘counter’ variable is initialised with the value 0, and this variable keeps track of the number of numbers in the string. This line of code, ‘for character in street:’, starts a loop and iterates over each string character one by one.

Then, for each character, it checks if the current character is numeric or not using the condition ‘if character.isnumeric():’. If the character is numeric, it increments the value of the counter variable by one using the ‘counter+=1’; otherwise, it skips and checks for the next character.

After iterating over all the string characters, this line of code prints the value of the ‘counter’ variable, representing the total number of numeric values in the string.

How to Count Numbers in a String Python using isalpha() Function

The alpha () method in Python checks if the given string contains the alphabet; if the string includes the alphabet, it returns True; otherwise, it returns False if the character in the string is not an alphabet.

READ:  How to convert a list to DataFrame in Python [9 ways]

The syntax is given below.

str.isalpha()

Where calling is ‘isalpha()’ function on the ‘str’ string that contains the alphabet character.

‘isalpha()’ works the same as the ‘isnumeric()’, but it checks for the alphabetic instead of the numeric value in the string.

Let’s take an example: suppose you have a string like ‘Chicago09876545’ and want to count the number of numeric characters in the string; you can use the code below.

string = "Chicago09876545"

alpha_count = 0

for char in string:
    if char.isalpha():
        alpha_count += 1

num_count = len(string)-alpha_count
print("Total numbers found:-", num_count)
How to Count Numbers in a String Python using isalpha() Function

Look at the output; the string ‘Chicago09876545’ contains 8 numbers. Let’s understand the code part of how it counts the numbers in the given string.

The ‘alpha_count’ variable is initialised with the value 0 and keeps track of the number of numbers in the string. This line of code, ‘for char in string:’, starts a loop and iterates over each string character individually.

Then, for each character, it checks if the current character is an alphabet or not using the condition ‘if char.isalpha():’. If the character is alphabet, it increments the value of the ‘alpha_count’ variable by one using the ‘alpha_count+=1’; otherwise, it skips and checks for the next character.

After that, the variable alpha_count contains the number of alphabet characters in the string, excluding the numeric character. So, the following line of code ‘len(string)-alpha_count’.

Computes the length of the string using the len(string), which returns the number of characters in the string, then the alpha_count variable is decreased from this computed length of the string.

As you know, the variable alpha_count contains the number of characters in the string. Using the len(string), the total number of characters is computed; then, subtract the alpha_count from the total number of characters to know the number of numeric values in the string.

READ:  Python for loop with index [4 Ways & Examples]

This is how to count the numbers in string Python using the isalpha() method.

Apart from this, you can use the method isdigit () to know if the characters in the string are digits. Then, you can count the number of digits in the string.

Conclusion

In this Python tutorial, you learned how to count numbers in string Python using Python’s isnumeric() and isalpha() functions.

You may like to read: