Skip to content

Instantly share code, notes, and snippets.

@revsuine
Last active April 18, 2018 15:01
Show Gist options
  • Save revsuine/8b6f1a6041a0a236d90f53ede2696d0f to your computer and use it in GitHub Desktop.
Save revsuine/8b6f1a6041a0a236d90f53ede2696d0f to your computer and use it in GitHub Desktop.
Simple helper function to return what substring a string starts with.
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