Convert First Letter of Every Word to Uppercase

Quick Overview

Write a function that uppercases the first character of each space-separated word in a sentence and leaves every other character as it is. It tests careful string scanning, word-boundary detection, and handling of words that start with digits, punctuation or capital letters.

Convert First Letter of Every Word to Uppercase

Company: AVEVA

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

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 ```python 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 ```text Input: s = "hello world from python" Output: "Hello World From Python" ``` The first letter of each of the four words is capitalized. ```text 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. ```text 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.

Overview: Write a function that uppercases the first character of each space-separated word in a sentence and leaves every other character as it is. It tests careful string scanning, word-boundary detection, and handling of words that start with digits, punctuation or capital letters.

AVEVA logo
AVEVA
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

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.