Exploring Substring Search in Python Strings

Introduction

Hello, and welcome to our analysis lesson. Today, we will be exploring a classic problem in the realm of string manipulations. We'll learn how to locate all occurrences of a substring within a larger string using Python. The techniques you will learn can be used in scenarios such as text processing and data analysis. Are you ready to dive in? Let's get started!

Task Statement and Description

Here's our challenge: we have two lists of strings of the same length, one containing the "original" strings and the other, the "substrings". We're to identify all occurrences of each substring within its corresponding original string and return a list of the starting indices of these occurrences. Remember, index counting should start from 0.

Example

If we take the following lists: Original List: ["HelloWorld", "LearningPython", "GoForBroke", "BackToBasics"] Substring List: ["loW", "ear", "o", "Ba"].

This will produce the following outputs: In "HelloWorld", "loW" starts at index 3. In "LearningPython", "ear" starts at index 1. In "GoForBroke", "o" appears at indices 1, 3, and 7. In "BackToBasics", "Ba" starts at indices 0 and 6.

So, if findSubString(["HelloWorld", "LearningPython", "GoForBroke", "BackToBasics"], ["loW", "ear", "o", "Ba"]) is called, the function should return

[
    "The substring 'loW' was found in the original string 'HelloWorld' at position(s) 3.",
    "The substring 'ear' was found in the original string 'LearningPython' at position(s) 1.",
    "The substring 'o' was found in the original string 'GoForBroke' at position(s) 1, 3, 7.",
    "The substring 'Ba' was found in the original string 'BackToBasics' at position(s) 0, 6."
]

Although this task seems reasonably straightforward, it may still feel daunting. Fear not! We will dissect it piece by piece.

Step-by-Step Solution: Initializing the Output

First, we need to create a place to store the result of our findings. Can you think of a Python data type that we could use for that? That's correct—a list would be perfect for this task!

def solution(orig_strs, substrs):
    result_arr = []

Pairing Strings and Finding First Occurrence

We use the built-in zip() function to create pairs of original strings and substrings. We then use the find() method to find the first occurrence of each substring in its related original string. Python string objects have a built-in method called str.find(substring, starting_index=0), which comes in handy here. It returns the lowest index of the substring in str that is greater than or equal to starting_index if found. Otherwise, it returns -1.

for original, substring in zip(orig_strs, substrs):
    start_pos = original.find(substring)

In original.find(substring), we pass the 'substring' that we want to locate. The function begins the search from the beginning as we have not specified a starting position.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal