Have a go at it. The editor and the docs panel are open, and your code is saved as you type. Running it needs a free account — you’ll come back to exactly what you wrote.
Working with Strings
A string is text. Strings come with methods, which are functions attached to the value itself, called with a dot:
" hello ".strip() # "hello" removes whitespace from both ends
"hello".upper() # "HELLO"
"HELLO".lower() # "hello"
"ada lovelace".title() # "Ada Lovelace"
"a,b,c".split(",") # ["a", "b", "c"]
"-".join(["a", "b"]) # "a-b"
len("hello") # 5Important: string methods never change the original. They return a new string.
name = " ada " name.strip() # returns "ada" name # still " ada " <- unchanged! name = name.strip() # this is how you keep the result
Your task:
Write clean_name(raw) that takes a messy name and tidies it:
So " grace hopper " becomes "Grace Hopper".
Example Tests
Extra spaces are removed and each word is capitalised
Input: {"raw":" grace hopper "}
Expected: "Grace Hopper"
Shouty input is brought back to normal capitalisation
Input: {"raw":"ADA LOVELACE"}
Expected: "Ada Lovelace"
A single name works too
Input: {"raw":" turing "}
Expected: "Turing"
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
passRun your code to see results
⌘↵ runs against the visible tests