diff --git a/strings/capitalize.py b/strings/capitalize.py index 628ebffc8852..32c0b1644ba5 100644 --- a/strings/capitalize.py +++ b/strings/capitalize.py @@ -13,12 +13,11 @@ def capitalize(sentence: str) -> str: >>> capitalize("") '' """ - if not sentence: - return "" # Capitalize the first character if it's a lowercase letter # Concatenate the capitalized character with the rest of the string - return sentence[0].upper() + sentence[1:] + # Slicing keeps this safe for empty strings. + return sentence[:1].upper() + sentence[1:] if __name__ == "__main__":