#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    11
    Rep Power
    0

    Question Computing with strings


    Hi everyone,

    I had a python question which I can't solve. I am not to good at this stuff, I admit. Here is the question.

    Write a program that allows the user to type in a phrase and then outputs the acronym for that phrase. The acronym should be all uppercase, even if the words in the phrase are not capitalized.

    Here is what I have so far. I know its totally wrong but I just am not sure what to do. I thought maybe you need to get the length of the phrase with len or something? I am totally lost, heh. I would appreciate any help in this matter.

    import string

    def main():
    print "This program takes a phrase and gives its acronym."
    print
    phrase = raw_input("Enter in the phrase: ")
    len(phrase)

    print "The acronym is",
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Hi Adam,

    First I need to ask you if this is a homework assignment, before I can give you a working example . In any case, what you should do is to loop over each word in the string you can do this by splitting the string on white-spaces and then looping over the returned list.

    Inside the loop, you could use the strip() method to remove any punctuation from around the word; this is recommended although not technically required!

    Edit: you can find an example of how to do this here:

    http://forums.devshed.com/t208855/s.html

    After that: take the first character of the word (using theWord[0]) and convert it to uppercase before appending it to a list defined outside of the loop.

    When the loop has finished you should have a list of all the first letters in your phrase, which can be joined together to form your acronym

    hope this helps,

    Mark.
    Last edited by netytan; February 6th, 2005 at 05:50 PM.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    11
    Rep Power
    0
    Thanks for the quick reply moderator Mark, heh. I will give what you said a try. As for the question its an example my instructor gave but I only half remember what he did because I didn't write it down. I am weird I like to do the examples, sometimes it helps me learn how to do the actually problems.
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    Code:
    >>> i = raw_input("Phrase: ")
    Phrase: i crashed my car
    >>> i = i.split(' ')
    >>> i
    ['i', 'crashed', 'my', 'car']
    >>> l = []
    >>> for item in i:
    	l.append(item[0].upper())
    >>> l
    ['I', 'C', 'M', 'C']
    Now, I myself can't think of a way to put those capital letters together, like ICMC. You could do:

    Code:
    if len(l) == 2:
        for item in l:
            print item+item
    elif len(l) == 3:
        for item in l:
            print item+item+item
    And you would do this maybe until len(1) equals 8. Guessing that the phrase isn't going to be long. Hope this helps a bit.
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Your very welcome user Adam . Anyway, if you have any questions dont hesitate to as. Or if you want a working example that you can follow along with I'd be more than happy to provide one.

    Have fun,

    Mark.
    programming language development: www.netytan.com Hula

  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    11
    Rep Power
    0
    For a newb like me a working example would be nice. Just to give you a little background on my programming experience, this is the first class I have taken in any programming language so I know nearly nothing about alot of this stuff so you can never simplify things too much for me, heh.
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Heres a copy of my Python shell. If you've never used this before it is basically a way to interact with Python and see the results right away. Anyway here it is, step by step.

    Code:
    >>> from string import punctuation
    >>> 
    >>> phrase = 'One working example... working'
    >>> acronym = []
    >>> 
    >>> for word in phrase.split():
    ...     word = word.strip(punctuation)
    ...     word = word.upper()
    ...     acronym.append(word[0])
    ... 
    >>> acronym
    ['O', 'W', 'E', 'W']
    >>> ''.join(acronym)
    'OWEW'
    >>> '.'.join(acronym)
    'O.W.E.W'
    >>>
    This could also be shortened into a list comprehension however for clarity i've left it like this .

    Mark.
    programming language development: www.netytan.com Hula

  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    11
    Rep Power
    0
    Thanks Mark. That example makes sense, now. What changes would have to be made to do the same thing but in the text editor? It seems like I couldn't get the acronym to have periods between letters if I used the text editor. The shell worked just fine.
  16. #9
  17. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Generally when you convert an example from the shell you need to remove the shell prompts (>>>) and the periods (...), as well as any output sent from the interpreter. You also need to remember to use the [b]print[/i] keyword if you want something to be displayed. So we end up with something like this:

    Code:
    from string import punctuation
    
    phrase = 'One working example... working'
    acronym = []
    
    for word in phrase.split():
        word = word.strip(punctuation)
        word = word.upper()
        acronym.append(word[0])
    
    print '.'.join(acronym)
    Night all,

    Mark.
    programming language development: www.netytan.com Hula

  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    11
    Rep Power
    0
    Thanks again!

IMN logo majestic logo threadwatch logo seochat tools logo