16

In Python, how do I make an acronym of a given string?

Like, input string:

'First Second Third'

Output:

'FST'

I am trying something like:

>>> for e in x:
        print e[0]

But it is not working... Any suggestions on how this can be done? I am sure there is a proper way of doing this but I can't seem to figure it out. Do I have to use re?

user225312
  • 126,773
  • 69
  • 172
  • 181

11 Answers11

20

Try

print "".join(e[0] for e in x.split())

Your loop actually loops over all characters in the string x. If you would like to loop over the words, you can use x.split().

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
19

If you want to use capitals only

>>>line = ' What AboutMe '
>>>filter(str.isupper, line)
'WAM'

What about words that may not be Leading Caps.

>>>line = ' What is Up '
>>>''.join(w[0].upper() for w in line.split())
'WIU'

What about only the Caps words.

>>>line = ' GNU is Not Unix '
>>>''.join(w[0] for w in line.split() if w[0].isupper())
'GNU'
kevpie
  • 25,206
  • 2
  • 24
  • 28
  • 1
    ''.join(w[0].upper() for w in line.split()) should be ''.join(w[0] for w in line.split()).upper() – kevpie Dec 04 '10 at 19:43
6

Without re:

>>> names = 'Vincent Vega Jules Winnfield'
>>> ''.join(x[0] for x in names.split())
'VVJW'
user225312
  • 126,773
  • 69
  • 172
  • 181
5

If you want to do things the way that is grammatically correct (regardless of locale), use title(), then filter():

acronym = filter(str.isupper, my_string.title())

title() is pretty awesome; it makes a string titlecased and is correct according to locale.

Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
3

Now for something a little bit different...

words = "There ain't no such thing as a free lunch."
acronym = ''.join(word[0] for word in words.upper().split())
print acronym
# TANSTAAFL

(TANSTAAFL is a fairly well-know one, BTW).

martineau
  • 119,623
  • 25
  • 170
  • 301
2
s = 'First Second Third'
x = s.split(' ')
for e in x:
    print e[0]

should do the trick.

vkris
  • 2,095
  • 7
  • 22
  • 30
2

Also you could use

re.split('\W')

to split the line/text on non-word characters. This might be a little bit more robust.

brown.2179
  • 1,750
  • 1
  • 12
  • 15
1

This is my suggestion so we can remove words like to, and , of as well as signs like',':

stopwords = ['to', 'a', 'for', 'by', 'an', 'am', 'the', 'so', 'it', 'and', "The"]
Phrase=input("please enter the phrase you need its acronym: ")
acro=""
for i in range(len(stopwords)):
    Phrase=Phrase.replace(stopwords[i]+' ',"")
    Phrase=Phrase.replace(',',' ')
Phrase=Phrase.upper()
Words=Phrase.split( )
for word in Words:
    acro = acro + word[0]
print(acro)
ElhamN
  • 11
  • 1
0

Here's how to do acronym with regular expression, leaving numbers as is:

import re
words = "internet explorer 10"
print re.sub(r"([a-zA-Z])[a-z,A-Z]+\s*",r"\1",words).upper()

IE10

Noam Manos
  • 15,216
  • 3
  • 86
  • 85
0

There is a python-based tool for this task, that suggests multiple acronyms, that can be installed via

pip install acronym

Source code repo is here: https://github.com/bacook17/acronym

MoreIT
  • 73
  • 3
-1

this is my suggestion

abbr=input("enter:")
list1=abbr.split()
print(list1)

for x in range(0,len(list1)):
    print(list1[x][:1],end="")
S.B
  • 13,077
  • 10
  • 22
  • 49
keyvan
  • 1
  • 1