

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program – All occurrences of Substring from the list of strings
When it is required to fetch all the occurrences of a substring from a list of strings, a list comprehension and the ‘startswith’ method is used.
Example
Below is a demonstration of the same −
my_string = "Python learn code test fun amazing object oriented" sub_string = "object" print("The string is : " ) print(my_string) print("The sub-string is : " ) print(sub_string) my_result = [index for index in range(len(my_string)) if my_string.startswith(sub_string, index)] print("The resultant string is : ") print(my_result)
Output
The string is : Python learn code test fun amazing object oriented The sub-string is : object The resultant string is : [35]
Explanation
A string is defined and is displayed on the console.
A sub string is defined and is displayed on the console.
A list comprehension is used to iterate over the string and check if the string starts with a specific value.
This is done using the ‘startswith’ method.
This is assigned to a result.
This is displayed as output on the console.
- Related Questions & Answers
- Python – All occurrences of Substring from the list of strings
- Python Program to Find Number of Occurrences of All Elements in a Linked List
- Python - Find all the strings that are substrings to the given list of strings
- Return an array with the number of nonoverlapping occurrences of substring in Python
- Extract numbers from list of strings in Python
- Python program to find the character position of Kth word from a list of strings
- How can we replace all the occurrences of a substring with another substring within a string in MySQL?
- Maximum Number of Occurrences of a Substring in C++
- Python Program – Strings with all given List characters
- Program to find longest common prefix from list of strings in Python
- Return a copy of the string with all occurrences of substring old replaced by new in Numpy
- Count occurrences of a substring recursively in Java
- How to remove empty strings from a list of strings in Python?
- Python Program to print strings based on the list of prefix
- Python program to sort strings by substring range
Advertisements