Ok well I've hit a snag... i'm trying to write a program that makes Acronyms from user input... I can get all the letters capitalize, but the problem i am having is, well, taking the first letter from each and puting them together to make an acronym!!!!!

Recommended Answers

All 6 Replies

you could use the split method

>>> "the dog is going home".split()
['the', 'dog', 'is', 'going', 'home']

I hope I did not ruin your fun , but here is the code I would use

userinput = raw_input('> ')
split_input = userinput.split()

x = ""
for i in split_input:
    x = x + i[0]  
    
print x

this code is a little more polished

userinput = raw_input('> ')
split_input = userinput.split()

x = ""
for i in split_input:
    x = x + i[0] + "."
    
print x.upper()

do acronyms even have periods in them?

do acronyms even have periods in them?

Most of the time they don't! Here is just one more way to do it ...

str1 = "senior health inFormation development"

# just in case it isn't all lower case
str2 = str1.lower()

# make a title-cased copy
str3 = str2.title()

# now build the acronym from the capitalized letters
str4 = ""
for char in str3:
    if char.isupper():
        str4 += char

print "Follow the inner workings:"
print "Original string:", str1
print "All lower case :", str2
print "Titelized      :", str3
print "Pick caps      :", str4

My problem is going the other way, like I can't remember what JPEG or JPG stands for! Hmm, maybe a Python dictionary would do that. Alright, ASCII stands for ...

Hi!

Here's a one-liner:

>>> str1 = "senior health inFormation development"
>>> '.'.join([word[0].upper() for word in str1.split()])
'S.H.I.D'

Regards, mawe

Great list comprehension example, mawe!
We just talked about list comprehension in a previous thread here, so I hope you don't mind, if I pick this apart for the beginning Pythonians amongst us.

# using list comprehension (mawe)
str1 = "senior health inFormation development"
acronym1 = '.'.join([word[0].upper() for word in str1.split()])
print acronym1  # S.H.I.D  note that join() does not add the final period

print

# let's pick this apart ...
# create a list of words from the original string
strList1 = str1.split()
print strList1
# this is the list comprehension part
# creates a list of upper case initial letters for each word
strList2 = [word[0].upper() for word in strList1]
print strList2
# create the acronym without periods this time, else use '.' in join()
acronym2 = ''.join(strList2)
print acronym2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.