-6
def get_initials(fullname):
  xs = (fullname)
  name_list = xs.split()

  initials = ""

  for name in name_list:  # go through each name
    initials += name[0].upper()  # append the initial
    ##       ^^ what is happening here?

  return initials

What is the += in this context? Is it incrementing the value in the list?

Lucan
  • 2,907
  • 2
  • 16
  • 30
Gagan
  • 1
  • 4
    there is no increment in this code, `+=` is not increment, but string concatenation here – Iłya Bursov May 07 '21 at 14:30
  • Does this answer your question? [What does it mean += in Python?](https://stackoverflow.com/questions/7721192/what-does-it-mean-in-python) – Zoe Jun 07 '21 at 19:46

1 Answers1

-1

The line initials += name[0].upper() # append the initial adds the first character to a string, the process:

  1. Split a string into a list (So john doe becomes ['john', 'doe'])
  2. Iterate over each item in that list
  3. For each item in that list, append to the empty string the first character, capitialized
    For example, for john, get the first character j and capitalize it as J
  4. Return the initials (JD in this case)
Anonymous
  • 738
  • 4
  • 14
  • 36