numpy string operations | count() function Last Updated : 23 Jan, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report numpy.core.defchararray.count(arr, sub, start=0, end=None) is another function for doing string operations in numpy.It returns an array with the number of non-overlapping occurrences of substring sub in the range start to end. Parameters: arr : array_like of str or unicode. sub : [str or unicode] The substring which to be searched. start : [ int, optional] The starting location in each string. end : [ int, optional] The ending location in each string. Returns : [ndarray] the number of non-overlapping occurrences of substring sub. Code #1 : Python3 # Python program explaining # numpy.char.count() method # importing numpy as geek import numpy as geek # input arrays in_arr = geek.array(['Sayantan', ' Sayan ', 'Sayansubhra']) print ("Input array : ", in_arr) # output arrays out_arr = geek.char.count(in_arr, sub ='an') print ("Output array: ", out_arr) Output: Input array : ['Sayantan' ' Sayan ' 'Sayansubhra'] Output array: [2 1 1] Code #2 : Python3 # Python program explaining # numpy.char.count() method # importing numpy as geek import numpy as geek # input arrays in_arr = geek.array(['Sayantan', ' Sayan ', 'Sayansubhra']) print ("Input array : ", in_arr) # output arrays out_arr = geek.char.count(in_arr, sub ='a', start = 1, end = 8) print ("Output array: ", out_arr) Output: Input array : ['Sayantan' ' Sayan ' 'Sayansubhra'] Output array: [3 2 2] Comment More infoAdvertise with us Next Article Numpy str_len() function J jana_sayantan Follow Improve Article Tags : Python Python-numpy Python numpy-String Operation Practice Tags : python Similar Reads numpy.count() in Python numpy.core.defchararray.count(arr, substring, start=0, end=None): Counts for the non-overlapping occurrence of sub-string in the specified range. Parameters: arr : array-like or string to be searched. substring : substring to search for. start, end : [int, optional] Range to search in. Returns : An 1 min read Numpy str_len() function numpy.char.str_len(arr) function is used for doing string operations in numpy. It returns the length of every string element wise. Parameters: arr : array_like of str or unicode.Input array. Returns : [ndarray] Output Array of integer. Code #1 : Python3 # Python program explaining # numpy.char.str_l 1 min read Python | Operator.countOf Operator.countOf() is used for counting the number of occurrences of b in a. It counts the number of occurrences of value. Syntax : Operator.countOf(freq = a, value = b) Parameters : freq : It can be list or any other data type which store value value : It is value for which we have to count the num 2 min read Numpy count_nonzero method | Python numpy.count_nonzero() function counts the number of non-zero values in the array arr. Syntax : numpy.count_nonzero(arr, axis=None) Parameters : arr : [array_like] The array for which to count non-zeros. axis : [int or tuple, optional] Axis or tuple of axes along which to count non-zeros. Default is 1 min read Python | Pandas Series.str.count() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas str.count() method is used to count occurrence of a string or regex pattern in 3 min read Count Substrings with number of 0s and 1s in ratio of X : Y Given a binary string S, the task is to count the substrings having the number of 0s and 1s in the ratio of X : Y Examples: Input: S = "010011010100", X = 3, Y = 2Output: 5Explanation: The index range for the 5 substrings are: (0, 4), (2, 6), (6, 10), (7, 11), (2, 11) Input: S = "10010101", X = 1, Y 7 min read Like