-3

This is my first post and I would be happy if someone could please explain to me why we need the result = "" section in the following Python code.

It is a basic piece of code that turns a phrase such as World Wide Web into WWW.

def initials(phrase):
    words = phrase.split()
    result = ""
    for word in words:
        result += word[0].upper()
    return result
solid.py
  • 2,782
  • 5
  • 23
  • 30
parmida
  • 21
  • 5
  • 4
    What happens if you don't include `result = ""`? What would you add the letters of the acronym to? – jkr Sep 02 '20 at 14:01
  • 1
    Welcome :) When posting, you should include samples of what you tried, what worked and what didn't. In this case, you could have tested what happened when removing the line you were curious about and included that in your question. – Gabriel Sep 02 '20 at 14:35
  • 1
    Duplicate of [Why do we need to declare the variable before we use it in some languages, but not in others?](https://stackoverflow.com/questions/23090669/why-do-we-need-to-declare-the-variable-before-we-use-it-in-some-languages-but-n) – TylerH Sep 02 '20 at 16:02
  • 1
    See also https://stackoverflow.com/questions/10134127/do-variables-have-to-be-defined-in-python-before-use – TylerH Sep 02 '20 at 16:04
  • 1
    And https://stackoverflow.com/questions/664294/is-it-possible-only-to-declare-a-variable-without-assigning-any-value-in-python – TylerH Sep 02 '20 at 16:04
  • 1
    And finally: https://stackoverflow.com/questions/11007627/python-variable-declaration – TylerH Sep 02 '20 at 16:04
  • Thank you for sending the links. – parmida Sep 02 '20 at 16:24

3 Answers3

3

+= in result += word[0].upper() implies you're trying to add a character to an existing string, if that existing string isn't pre-defined, it will throw an error.

It can also be written as follows:

result = result + word[0].upper()

Also be curious, try running the code with that portion commented out. Sticks better when you experience it yourself.

SamAko
  • 3,485
  • 7
  • 41
  • 77
2

It's acting as a temporary container for the strings. Here's a version without the result variable.

def initials(phrase):
    words = phrase.split()
    return "".join([word[0].upper() for word in words])
solid.py
  • 2,782
  • 5
  • 23
  • 30
  • Please use code fences (```) to mark your source code, so that it will be formatted appropriately. Otherwise, it will all appear in one long line. Another user fixed this for you. – Daniel Werner Sep 02 '20 at 14:43
2

The result needs to be initialised because you're adding something to it every iteration of the loop. Consider this bit:

for word in words:
    result += word[0].upper()

The += operator takes result, adds something on, and assigns it back to result. So in your example what's happening every loop is:

# Before loop 1
result = ""
# After loop 1
result = "W"
# After loop 2
result = "WW"

If you don't initialise result, then the += operator makes so sense on the first iteration.

Alan
  • 1,746
  • 7
  • 21