easy +8 pts

User Friendly Message

Convert a raw input string into a clean, human-friendly message with proper formatting.

Write a function `format_message(text: str) -> str` that takes a raw input string and returns a polished, user-friendly version by applying the following rules: 1. Strip leading and trailing whitespace. 2. Replace any sequence of multiple spaces (two or more) with a single space. 3. Ensure the string ends with exactly one period. If it already ends with a period, leave it; otherwise append a period. 4. Capitalize the first letter of the string. All other letters remain unchanged. Your function must return the transformed string. The input may be any string, including empty or whitespace-only strings. Examples: - `format_message(" hello world ")` returns `"Hello world."` - `format_message("already good.")` returns `"Already good."` - `format_message(" ")` returns `"."` (after stripping, the string is empty; then capitalize (no change) and append a period).

Constraints

Input string length is at most 1000 characters. The string may contain any printable ASCII characters.

Example

>>> format_message("  hello   world  ")
'Hello world.'
>>> format_message("already good.")
'Already good.'
>>> format_message("   ")
'.'
8 points ~12 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Think about stripping first, then collapsing multiple spaces.
Check if the last character is a period before appending one.
To capitalize, use `text[0].upper() + text[1:]` after cleaning.
An empty string has no first letter, so handle that case carefully.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.