Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

Find All Occurrences of a Substring in a String in Python

#defining string and substring
str1 = "This dress looks good; you have good taste in clothes."
substr = "good"

#occurrence of word 'good' in whole string
count1 = str1.count(substr)
print(count1)

#occurrence of word 'good' from index 0 to 25
count2 = str1.count(substr,0,25)
print(count2)

# output -
# 2
# 1
Source by www.delftstack.com #
 
PREVIOUS NEXT
Tagged: #Find #All #Occurrences #Substring #String #Python
ADD COMMENT
Topic
Name
3+1 =