Finding Text in a String with a Custom Function in Python

2 min read .

In software development, finding specific text within a larger string is a common task. While Python provides various built-in methods for text searching, sometimes you need a custom function to get more specific results. Will show you how to create a FindText function in Python to locate and return text around a keyword within a larger string.

1. Introduction to the FindText Function

The FindText function is designed to find a substring within a larger string and return a portion of text around the found substring. You can also specify the number of characters to include before and after the found substring.

Here is the implementation of the FindText function:

def FindText(text, f, space=100):
    tf = f
    find = text.find(tf)

    if find > -1:
        cl = max(find - space, 0)
        cr = min(len(text), find + len(tf) + space)
        
        return text[cl: cr]
    else:
        return 'not found'

2. Explanation of the Implementation

Let’s break down the steps in the FindText function:

  • text.find(tf): Searches for the substring tf in the string text. If found, it returns the index of the first occurrence. If not found, it returns -1.

  • Determine the Left Bound (cl):

    • cl is set to find - space if it is greater than 0. This ensures that we don’t get a negative index.
    • Otherwise, cl is set to 0 (the start of the string).
  • Determine the Right Bound (cr):

    • cr is set to find + len(tf) + space if it is less than the length of the string. This ensures that we include enough characters after the substring.
    • Otherwise, cr is set to the length of the string.
  • Return the Result:

    • If the substring is found, the function returns the portion of the string between cl and cr.
    • If not found, it returns 'not found'.

3. Example Usage

Suppose we have a large string and want to find text around a specific substring.

text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
substring = "consectetur"
result = FindText(text, substring, space=20)
print(result)

The output might be:

"m dolor sit amet, consectetur adipiscing elit. Sed do eiusmod"
  • In this example, the FindText function finds the substring "consectetur" and returns 20 characters before and after it.

4. Case When the Substring is Not Found

If the substring you are searching for is not found within the larger string, the function will return 'not found':

text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
substring = "python"
result = FindText(text, substring)
print(result)  # Output: 'not found'

Conclusion

The FindText function is an example of how to create a custom function for substring search within a larger string with more control over the results. By using this function, you can customize how much context you want to include in the search results. This function is useful in various cases, such as searching for text within large documents or performing text data analysis.

Tags:
Python

See Also

chevron-up