start_case()
Convert a string into Start Case: - Splits on spaces, punctuation, and camelCase - Capitalizes each word (preserves ALLCAPS words) - Drops apostrophes
Implementation
Args: string: The input string Returns: The Start Cased string
Example
start_case("helloWorld_test")
Expected output: "Hello World Test"
Source Code
def start_case(string: str) -> str:
if string is None:
return ""
s = str(string).strip()
if not s:
return ""
# camelCase splits
s = re.sub(r"([A-Z]+)([A-Z][a-z])", r"\1 \2", s)
s = re.sub(r"([a-z0-9])([A-Z])", r"\1 \2", s)
# drop apostrophes
s = s.replace("'", "")
# non-alphanumerics → spaces
s = re.sub(r"[^A-Za-z0-9]+", " ", s)
words = [w for w in s.split(" ") if w]
def _cap(w):
return w.upper() if w.isupper() else w.lower().capitalize()
return " ".join(_cap(w) for w in words)