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.

6

There are 6 best solutions below

0
On BEST ANSWER

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.

0
On
name = input("Enter uppercase with lowercase name")

print(f'the original string = ' + name)

def uppercase(name):
    res = [char for char in name if char.isupper()]
    print("The uppercase characters in string are : " + "".join(res))

uppercase(name)
0
On

works for me this way:

>>> a = "What would you like to acronymize?"
>>> a.split()
['What', 'would', 'you', 'like', 'to', 'acronymize?']
>>> ''.join([i[0] for i in a.split()]).upper()
'WWYLTA'
>>> 
0
On

Say input is "Federal Bureau of Agencies"

Typing third[0] gives you the first element of the split, which is "Federal". You want the first element of each element in the sprit. Use a generator comprehension or list comprehension to apply [0] to each item in the list:

val = input("What would you like to acronymize? ")
print("".join(word[0] for word in val.upper().split()))

In Python, it would not be idiomatic to use an explicit loop here. Generator comprehensions are shorter and easier to read, and do not require the use of an explicit accumulator variable.

0
On

One intuitive approach would be:

  1. get the sentence using input (or raw_input in python 2)
  2. split the sentence into a list of words
  3. get the first letter of each word
  4. join the letters with a space string

Here is the code:

sentence = raw_input('What would you like to acronymize?: ')
words = sentence.split() #split the sentece into words
just_first_letters = [] #a list containing just the first letter of each word

#traverse the list of words, adding the first letter of
#each word into just_first_letters
for word in words:
    just_first_letters.append(word[0])
result = " ".join(just_first_letters) #join the list of first letters
print result
0
On
#acronym2.py
#illustrating how to design an acronymn
import string
def main():
    sent=raw_input("Enter the sentence: ")#take input sentence with spaces 
    
    for i in string.split(string.capwords(sent)):#split the string so each word 
                                                 #becomes 
                                                 #a string
        print string.join(i[0]),                 #loop through the split 
                                                 #string(s) and
                                                 #concatenate the first letter 
                                                 #of each of the
                                                 #split string to get your 
                                                 #acronym
    
        
    
main()