Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Convert a string to title case

Transform each space-separated word so its first character is uppercase and the rest are lowercase, preserving spacing.

Python practice15 minString PatternsIntermediateLast updated March 18, 2026

Problem statement

Write a function title_case(s) that returns a new string where each word (defined as a sequence of characters separated by space characters) has its first character converted to uppercase and the remaining characters converted to lowercase. All original spacing (including leading, trailing, and multiple intermediate spaces) must be preserved exactly. Notes: - Words are delimited only by the space character (' '). - If a token between spaces is an empty string (because of consecutive spaces), it should remain empty so the spacing is preserved. - If the input string is empty, return an empty string.

Task

Implement a function that converts a string to title case while preserving original spacing.

Examples

Simple sentence

Input

title_case("hello world")

Output

Hello World

Each word's first letter is uppercased and the rest lowercased.

Input format

A single string s passed as an argument to title_case(s).

Output format

Return a single string with title-cased words and original spacing preserved.

Constraints

- 0 <= len(s) <= 2000 - Only split on the space character ' ' to determine words - Do not collapse or normalize whitespace

Samples

Sample 1

Input

title_case(" leading and multiple spaces ")

Output

Leading And Multiple Spaces

Leading, trailing, and multiple spaces are preserved; each non-empty token is title-cased.