23. Initials
Write a function initials(name) that takes a person's name and returns its initials as a string: the first letter
of each word, as a capital, followed by a dot. Don't put spaces between them.
Every word counts, short ones too: Guido van Rossum gives G.V.R.. Words can be separated by more than one space,
with spaces at the start or end; those don't change the answer. If name is empty, return an empty string "".
Example 1
name = "ada lovelace""A.L."Example 2
name = "Guido van Rossum""G.V.R."Example 3
name = " margaret hamilton ""M.H."Constraints
namehas 0 to 100 characters.- It holds only letters and spaces.
Submit also runs 3 more tests you can't see.
Practises String Methods (lesson 5).
name.split()gives the words, skipping extra spaces, and an empty list for an empty name.- For each word,
word[0]is its first letter and.upper()makes it a capital. Add"."after it. - Build the answer in a string that starts as
"", adding one initial per word. With no words it stays"", which is right for an empty name.
Solve it without hints for 3 stars, with hints for 2, after seeing the solution for 1.
One way to solve it, once you've had a go.
Solving it after seeing the solution earns 1 star.
Your Submits show here, with their code, so you can go back to one. They stay in this browser.
def initials(name):
"""Turn a name into its initials, like "A.L.".
name: str, words separated by spaces; may be empty
Returns: str, each word's first letter as a capital,
followed by a dot
"""
# Write your code here
pass
Run your code to see how it does on the test cases, or Submit to check every test.