Practice: Initials

Practice: Initials

This exercise combines split, indexing, upper, and a string accumulator.

Exercise

  1. Define a function named initials with one parameter, full_name.
  2. It returns the initials: the first letter of each word, uppercased, each followed by a period.

initials("Ada Lovelace") returns "A.L.". initials("grace brewster murray hopper") returns "G.B.M.H.". initials("Plato") returns "P.".

Run your code to see the output, then press Submit.

Hints

  • Split the name into words, then loop over the words.
  • For each word, add its first character (uppercased) and a period to the accumulator.

Tests

import unittest


class TestInitials(unittest.TestCase):
    def test_two_names(self):
        self.assertEqual(initials("Ada Lovelace"), "A.L.")

    def test_lowercase_names_are_uppercased(self):
        self.assertEqual(initials("grace brewster murray hopper"), "G.B.M.H.")

    def test_single_name(self):
        self.assertEqual(initials("Plato"), "P.")
def initials(full_name):
    result = ""
    for word in full_name.split():
        result += word[0].upper() + "."
    return result


print(initials("Ada Lovelace"))
print(initials("grace brewster murray hopper"))
Solution hidden. Give it a real try first.

Sign in to join the discussion.

No comments yet. Be the first to ask a question.

main.py
Console
Press Run to execute your code, or Submit to test and complete this problem.