Working with Strings

~8 mincode completion

Write clean_name(raw) that takes a messy name and tidies it:

  1. remove whitespace from both ends
  2. capitalise it properly

So " grace hopper " becomes "Grace Hopper".

Examples

Extra spaces are removed and each word is capitalised

Input
clean_name(" grace hopper ")
Output
"Grace Hopper"

Shouty input is brought back to normal capitalisation

Input
clean_name("ADA LOVELACE")
Output
"Ada Lovelace"

A single name works too

Input
clean_name(" turing ")
Output
"Turing"

Hints

Hint 1

Work directly with the arguments raw and return the result rather than printing it.

Hint 2

Do not forget to use return value of strip. That step is easy to skip.

Requirements

  • raw: a string that may have extra spaces and odd capitalisation

  • Return the trimmed, properly capitalised name.

Constraints

  • Standard library only, no imports required

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~8 min

3 employers weight this skill

2 frontier labs, 1 AI product company. Top match scores 80.

Python
def clean_name(raw):
    """
    Tidy up a name typed by a user.

    Args:
        raw: a string that may have extra spaces and odd capitalisation

    Returns:
        The trimmed, properly capitalised name.
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.