Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

print the number of times that the substring occurs in the given string

nStr = '000123000123'
pattern = '123'
count = 0
flag = True
start = 0

while flag:
    a = nStr.find(pattern, start)  # find() returns -1 if the word is not found, 
    #start i the starting index from the search starts(default value is 0)
    if a == -1:          #if pattern not found set flag to False
        flag = False
    else:               # if word is found increase count and set starting index to a+1
        count += 1        
        start = a + 1
print(count)
Comment

check how many times a substring appears in a string

a = '128532012'
print(a.count('0'))
#Prints: 1

print(a.count('12'))
#Prints: 2
Comment

PREVIOUS NEXT
Code Example
Python :: jupyter notebook set default directory 
Python :: how to convert png to pdf with python 
Python :: python to golang 
Python :: label encode one column pandas 
Python :: check django version 
Python :: python boxplot show mean 
Python :: trimming spaces in string python 
Python :: python check if input is between two values 
Python :: scoop bucket add extras 
Python :: python datetime to timestamp 
Python :: huggingface default cache dir 
Python :: python pynput letter key pressed 
Python :: python dedent 
Python :: Socket Programming Client Side 
Python :: Get all columns with particular name in string 
Python :: python ignore exception 
Python :: define variable with if statement python 
Python :: python how to change size of a window 
Python :: python remove duplicates from 2d list 
Python :: how to find the text inside button in tkinter 
Python :: remove blank spaces from a list python 
Python :: get last element of array python 
Python :: mad scipy 
Python :: how to change kay bindings in pycharm 
Python :: sin and cos in python 
Python :: telnet python 
Python :: binomial coefficient 
Python :: tkinter hello world 
Python :: how to rename columns in python 
Python :: python post request 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =