📌  相关文章
📜  在 Python 代码示例中查找字符串中子字符串的起始索引的所有出现次数

📅  最后修改于: 2022-03-11 14:45:06.368000             🧑  作者: Mango

代码示例1
# 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]