Last active
April 18, 2018 15:01
-
-
Save revsuine/8b6f1a6041a0a236d90f53ede2696d0f to your computer and use it in GitHub Desktop.
Simple helper function to return what substring a string starts with.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_startswith_substring(string: str, substrings): | |
""" | |
Gets the substring that another string starts with. | |
:param string: The larger string, eg "The quick brown fox jumps over the lazy dog." | |
:param substrings: The list or tuple of substrings you want to check against, e.g. ["foo", "bar", "Th"] | |
:return: If found, a string (that comes from substrings). If not found, None. e.g. "Th" | |
:raises: TypeError if substrings is not a list or tuple. | |
""" | |
if not (isinstance(substrings, list) or isinstance(substrings, tuple)): | |
raise TypeError | |
for item in substrings: | |
if string.startswith(item): | |
return item | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment