0

so i'm trying to make a program in Python PyScripter 3.3 that takes input, and converts the input into an acronym. This is what i'm looking for.

your input: center of earth

programs output: C.O.E.

I don't really know how to go about doing this, I am looking for not just the right answer, but an explanation of why certain code is used, thanks..

What I have tried so far:

def first_letters(lst):
        return [s[:1] for s in converted]



def main():

     lst = input("What is the phrase you wish to convert into an acronym?")
     converted = lst.split().upper()

Beyond here I am not really sure where to go, so far I know I need to captialize the input, split it into separate words, and then beyond that im not sure where to go...

Cole Readman
  • 23
  • 1
  • 8
  • You can try looking at this post: [Python SubString](http://stackoverflow.com/questions/663171/is-there-a-way-to-substring-a-string-in-python) – lgraham076 Jun 04 '15 at 20:52
  • I have the feeling that you do know how to do it - you just haven't laid out the specific steps of the algorithm you use. Try to go through it on paper, using simple steps that have a Python equivalent (like built-ins, string methods, etc.). – TigerhawkT3 Jun 04 '15 at 20:55

3 Answers3

1
>>> import re
>>> s = "center of earth"
>>> re.sub('[a-z ]+', '.', s.title())
'C.O.E.'

>>> "".join(i[0].upper() + "." for i in s.split())
'C.O.E.'
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • 1
    Or, more simply, `".".join(i[0].upper() for i in s.split())` – Ionut Hulub Jun 04 '15 at 20:55
  • I am very new to python, and after searching the forums for this exact question, I am looking for some help that would include an explanation, not just an answer. I don't want an answer, I want to understand how to go about doing this so I don't need to ask for help in the future. – Cole Readman Jun 04 '15 at 20:56
  • 1
    @IonutHulub, that leaves off the final '.' – John La Rooy Jun 04 '15 at 20:59
  • @JohnLaRooy so just add `+ '.'` at the end. It's a matter of personal taste I guess. – Ionut Hulub Jun 04 '15 at 21:01
1

Since you want an explanation and not just an answer:

>>> s = 'center of earth'
>>> s = s.split()  # split it into words
>>> s
['center', 'of', 'earth']
>>> s = [i[0] for i in s]  # get only the first letter or each word
>>> s
['c', 'o', 'e']
>>> s = [i.upper() for i in s]  # convert the letters to uppercase
>>> s
['C', 'O', 'E']
>>> s = '.'.join(s)  # join the letters into a string
>>> s
'C.O.E'
>>> s = s + '.'  # add the dot at the end
>>> s
'C.O.E.'
Ionut Hulub
  • 6,180
  • 5
  • 26
  • 55
1

I like Python 3.

>>> s = 'center of earth'
>>> print(*(word[0] for word in s.upper().split()), sep='.', end='.\n')
C.O.E.
  1. s = 'center of earth' - Assign the string.
  2. s.upper() - Make the string uppercase. This goes before split() because split() returns a list and upper() doesn't work on lists.
  3. .split() - Split the uppercased string into a list.
  4. for word in - Iterate through each element of the created list.
  5. word[0] - The first letter of each word.
  6. * - Unpack this generator and pass each element as an argument to the print function.
  7. sep='.' - Specify a period to separate each printed argument.
  8. end='.\n' - Specify a period and a newline to print after all the arguments.
  9. print - Print it.

As an alternative:

>>> s = 'center of earth'
>>> '.'.join(filter(lambda x: x.isupper(), s.title())) + '.'
'C.O.E.'
  1. s = 'center of earth' - Assign the string.
  2. s.title() - Change the string to Title Case.
  3. filter - Filter the string, retaining only those elements that are approved by a predicate (the lambda below).
  4. lambda x: x.isupper() - Define an anonymous inline function that takes an argument x and returns whether x is uppercase.
  5. '.'.join - Join all the filtered elements with a '.'.
  6. + '.' - Add a period to the end.

Note that this one returns a string instead of simply printing it to the console.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97