Simplify substring search
Documentation Index
Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt
Use this file to discover all available pages before exploring further.
Sourcery refactoring id: simplify-substring-search
Section titled “Sourcery refactoring id: simplify-substring-search”Description:
Section titled “Description:”Simplify finding if substrings are present in strings by using in
Before:
Section titled “Before:”my_str = "Hello world"if my_str.find("ello") == -1: print("Not Found!")After:
Section titled “After:”my_str = "Hello world"if "ello" not in my_str: print("Not Found!")Before:
Section titled “Before:”my_str = "Hello world"if my_str.count("ello") > 0: print("Found!")After:
Section titled “After:”my_str = "Hello world"if "ello" in my_str: print("Found!")Explanation:
Section titled “Explanation:”Making use of Python’s in operator for detecting if a substring is present in
a string is more readable than using the find or count methods, and is also
suggested in the
documentation. These
methods are more suitable for cases where you need more information about the
substring’s location or the number of times it appears.