Open In App

Python | Frequency of numbers in String

Last Updated : 17 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Strings, we can have a problem in which we need to check how many of numerics are present in strings. This is a common problem and have application across many domains like day-day programming and data science. Lets discuss certain ways in which this task can be performed. 

Method #1 : Using re.findall() + len() The combination of above functions can be used to perform this task. In this, we check for all numbers and put in list using findall() and the count is extracted using len(). 

Python3




# Python3 code to demonstrate working of
# Frequency of numbers in String
# Using re.findall() + len()
import re
 
# initializing string
test_str = "geeks4feeks is No. 1 4 geeks"
 
# printing original string
print("The original string is : " + test_str)
 
# Frequency of numbers in String
# Using re.findall() + len()
res = len(re.findall(r'\d+', test_str))
 
# printing result
print("Count of numerics in string : " + str(res))


Output : 

The original string is : geeks4feeks is No. 1 4 geeks
Count of numerics in string : 3

Time complexity: The time complexity of re.findall() function is O(n), where n is the length of the input string.

Auxiliary space: The auxiliary space used by re.findall() function is O(n), where n is the length of the input string.

  Method #2 : Using sum() + findall() The combination of above functions can also be used to solve this problem. In this, we cumulate the sum using sum(). The task of findall() is to find all the numerics. 

Python3




# Python3 code to demonstrate working of
# Frequency of numbers in String
# Using re.findall() + sum()
import re
 
# initializing string
test_str = "geeks4feeks is No. 1 4 geeks"
 
# printing original string
print("The original string is : " + test_str)
 
# Frequency of numbers in String
# Using re.findall() + sum()
res = sum(1 for _ in re.finditer(r'\d+', test_str))
 
# printing result
print("Count of numerics in string : " + str(res))


Output : 

The original string is : geeks4feeks is No. 1 4 geeks
Count of numerics in string : 3

Method #3 : Using isdigit() method

Python3




# Python3 code to demonstrate working of
# Frequency of numbers in String
 
# initializing string
test_str = "geeks4feeks is No. 1 4 geeks"
 
# printing original string
print("The original string is : " + test_str)
 
# Frequency of numbers in String
res=0
for i in test_str:
    if(i.isdigit()):
        res+=1
# printing result
print("Count of numerics in string : " + str(res))


Output

The original string is : geeks4feeks is No. 1 4 geeks
Count of numerics in string : 3

The Time and Space Complexity for all the methods are the same:

Time Complexity: O(n)

Auxiliary Space: O(n)

Method #4 : Without using any builtin methods

Python3




# Python3 code to demonstrate working of
# Frequency of numbers in String
 
# initializing string
test_str = "geeks4feeks is No. 1 4 geeks"
 
# printing original string
print("The original string is : " + test_str)
 
# Frequency of numbers in String
res=0
digits="0123456789"
for i in test_str:
    if(i in digits):
        res+=1
# printing result
print("Count of numerics in string : " + str(res))


Output

The original string is : geeks4feeks is No. 1 4 geeks
Count of numerics in string : 3

Method #5 : Using filter()+list()+len()+isdigit()+lambda functions

Python3




# Python3 code to demonstrate working of
# Frequency of numbers in String
 
# initializing string
test_str = "geeks4feeks is No. 1 4 geeks"
 
# printing original string
print("The original string is : " + test_str)
 
# Frequency of numbers in String
res = len(list(filter(lambda x: x.isdigit(), test_str)))
 
# printing result
print("Count of numerics in string : " + str(res))


Output

The original string is : geeks4feeks is No. 1 4 geeks
Count of numerics in string : 3

Time Complexity: O(n)

Auxiliary Space: O(n)

Method #6 : Using map

Python3




# Python3 code to demonstrate working of
# Frequency of numbers in String
  
# initializing string
test_str = "geeks4feeks is No. 1 4 geeks"
  
# printing original string
print("The original string is : " + test_str)
  
# Frequency of numbers in String
res = sum(map(str.isdigit, test_str))
  
# printing result
print("Count of numerics in string : " + str(res))


Output

The original string is : geeks4feeks is No. 1 4 geeks
Count of numerics in string : 3

Time Complexity: O(n)

Auxiliary Space: O(1)

Note: In this approach, we are using map() function to convert all the characters of the string into True or False based on whether it is a digit or not. Then using sum() we are counting the number of True values, which is equivalent to counting the number of digits in the string.



Similar Reads

Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
Given a string S, the task is to find the prefix of string S with the maximum possible length such that frequency of each character in the prefix is at most the number of characters in S with minimum frequency. Examples: Input: S = 'aabcdaab' Output: aabcd Explanation: Frequency of characters in the given string - {a: 4, b: 2, c: 1, d: 1} Minimum f
8 min read
Python - Find the frequency of numbers greater than each element in a list
Given a list, a new list is constructed that has frequency of elements greater than or equal to it, corresponding to each element of the list. Input : test_list = [6, 3, 7, 1, 2, 4] Output : [2, 4, 1, 6, 5, 3] Explanation : 6, 7 are greater or equal to 6 in list, hence 2. Input : test_list = [6, 3, 7] Output : [2, 3, 1] Explanation : 6, 7 are great
8 min read
Find frequency of each word in a string in Python
Write a python code to find the frequency of each word in a given string. Examples: Input : str[] = "Apple Mango Orange Mango Guava Guava Mango" Output : frequency of Apple is : 1 frequency of Mango is : 3 frequency of Orange is : 1 frequency of Guava is : 2 Input : str = "Train Bus Bus Train Taxi Aeroplane Taxi Bus" Output : frequency of Train is
7 min read
Python | Count all prefixes in given string with greatest frequency
Given a string, print and count all prefixes in which first alphabet has greater frequency than second alphabet.Take two alphabets from the user and compare them. The prefixes in which the alphabet given first has greater frequency than the second alphabet, such prefixes are printed, else the result will be 0. Examples : Input : string1 = "geek", a
4 min read
Python | Frequency of each character in String
Given a string, the task is to find the frequencies of all the characters in that string and return a dictionary with key as the character and its value as its frequency in the given string. Method #1 : Naive method Simply iterate through the string and form a key in dictionary of newly occurred element or if element is already occurred, increase i
6 min read
Python | Frequency of substring in given string
Finding a substring in a string has been dealt with in many ways. But sometimes, we are just interested to know how many times a particular substring occurs in a string. Let's discuss certain ways in which this task is performed. Method #1: Using count() This is a quite straightforward method in which this task is performed. It simply counts the oc
6 min read
Python | Construct string from character frequency tuple
Sometimes, while working with data, we can have a problem in which we need to perform construction of string in a way that we have a list of tuple having character and it's corresponding frequency and we require to construct a new string from that. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop This is brute
5 min read
Python | Maximum frequency character in String
This article gives us the methods to find the frequency of maximum occurring character in a python string. This is quite important utility nowadays and knowledge of it is always useful. Let's discuss certain ways in which this task can be performed. Method 1 : Naive method + max() In this method, we simply iterate through the string and form a key
4 min read
Python - Prefix frequency in string List
Sometimes, while working with Python lists, we can have a problem in which we need to get the count of strings that start with a particular substring. This can have an application in web development and general programming. Let us discuss certain ways in which this task can be performed. Method #1 : Using loop + startswith() The combination of the
6 min read
Python - Bigrams Frequency in String
Sometimes while working with Python Data, we can have problem in which we need to extract bigrams from string. This has application in NLP domains. But sometimes, we need to compute the frequency of unique bigram for data collection. The solution to this problem can be useful. Lets discuss certain ways in which this task can be performed. Method #1
4 min read
Practice Tags :