Horje
Find All Occurrences of start indices of the substrings in a String in Python Code Example
Find All Occurrences of start indices of the substrings in a String in Python
# defining string 
str1 = "This dress looks good; you have good taste in clothes."
  
# defining substring
substr = "good"
  
# printing original string 
print("The original string is : " + str1)
  
# printing substring 
print("The substring to find : " + substr)
  
# using list comprehension + startswith()
# All occurrences of substring in string 
res = [i for i in range(len(str1)) if str1.startswith(substr, i)]
  
# printing result 
print("The start indices of the substrings are : " + str(res))

# 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]




Python

Related
python iterate through list Code Example python iterate through list Code Example
string to date in BQ Code Example string to date in BQ Code Example
come mettere una scelta su python Code Example come mettere una scelta su python Code Example
reference other libraries in library Code Example reference other libraries in library Code Example
how to check local endianness with Python ? Code Example how to check local endianness with Python ? Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
6