Given a string s representing a sentence, return a new string where the first character of every word is converted to uppercase, leaving the rest of each word unchanged.
Words are separated by single spaces. If the first character of a word is not a lowercase English letter (for example, a digit, punctuation, or an already uppercase letter), it stays as it is.
Function Signature
def capitalize_words(s: str) -> str:
Rules
-
A word is a maximal run of non-space characters. A character starts a word when it is at index 0 or the character before it is a space.
-
Only a lowercase ASCII letter
a
to
z
at the start of a word changes, and it becomes its uppercase form. Every other character, including all letters after the first one in a word, is copied unchanged.
Constraints
-
1 <= len(s) <= 10^4
-
s
consists of printable ASCII characters.
-
s
does not contain leading or trailing spaces.
-
Words in
s
are separated by a single space.
Examples
Input: s = "hello world from python"
Output: "Hello World From Python"
The first letter of each of the four words is capitalized.
Input: s = "the qUICK brown fOX"
Output: "The QUICK Brown FOX"
Only the first character of each word changes. The uppercase letters later in "qUICK" and "fOX" stay as they are.
Input: s = "4 score and (seven) years"
Output: "4 Score And (seven) Years"
The words "4" and "(seven)" start with a digit and a parenthesis, so they are copied unchanged. The other three words get a capital first letter.