“Trouvez toutes les occurrences d'une sous-chaîne dans une chaîne en python” Réponses codées

Trouvez toutes les occurrences d'une sous-chaîne dans une chaîne en python

import re 
 
# defining string  
str1 = "This dress looks good; you have good taste in clothes."
 
#defining substring 
substr = "good"
 
print("The original string is: " + str1) 
 
print("The substring to find: " + substr) 
 
result = [_.start() for _ in re.finditer(substr, str1)] 
 
print("The start indices of the substrings are : " + str(result))

# Output - 
# The original string is: This dress looks good; you have good taste in clothes.
# The substring to find: good
# The start indices of the substrings are : [17, 34]
Rajitha Amarasinghe

Trouvez toutes les occurrences d'une sous-chaîne dans une chaîne en 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
Rajitha Amarasinghe

Réponses similaires à “Trouvez toutes les occurrences d'une sous-chaîne dans une chaîne en python”

Questions similaires à “Trouvez toutes les occurrences d'une sous-chaîne dans une chaîne en python”

Plus de réponses similaires à “Trouvez toutes les occurrences d'une sous-chaîne dans une chaîne en python” dans Python

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code