My code
beginning = input("What would you like to acronymize? : ")
second = beginning.upper()
third = second.split()
fourth = "".join(third[0])
print(fourth)
I can't seem to figure out what I'm missing. The code is supposed to the the phrase the user inputs, put it all in caps, split it into words, join the first character of each word together, and print it. I feel like there should be a loop somewhere, but I'm not entirely sure if that's right or where to put it.
When you run the code third[0]
, Python will index the variable third
and give you the first part of it.
The results of .split()
are a list of strings. Thus, third[0]
is a single string, the first word (all capitalized).
You need some sort of loop to get the first letter of each word, or else you could do something with regular expressions. I'd suggest the loop.
Try this:
fourth = "".join(word[0] for word in third)
There is a little for
loop inside the call to .join()
. Python calls this a "generator expression". The variable word
will be set to each word from third
, in turn, and then word[0]
gets you the char you want.