new to site and python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kasrav
    New Member
    • Apr 2007
    • 16

    #1

    new to site and python

    i dnt have much access to the internet so thats why i take long to reply sorry about that. strings are a big headache fro me so i dnt know if u could give any ideas of how to go about them e.g, if i had to write a program where u have to enter a phrase and then the program calculates and outputs the acronym derived from that phrase. all help appreaciated thanks

    --------------------------------------------------------------------------------
  • dshimer
    Recognized Expert New Member
    • Dec 2006
    • 136

    #2
    Here are a couple of things to look at. You could take the phrase and separate it into a list of words using split()
    Code:
    >>> p='this is a phrase'
    >>> p.split()
    ['this', 'is', 'a', 'phrase']
    Then you could use a for loop to pull out the first letter of each word. Remember that the first letter of a string has an index of 0, second is 1, etc.
    Code:
    >>> for w in p.split():
    ... 	print w[0]
    ... 	
    t
    i
    a
    p
    In this example I just printed the letter, but you could build a new word out of it (lets use a for acronym)
    Code:
    >>> a=''
    >>> for w in p.split():
    ... 	a=a+w[0]
    ... 
    >>> print a
    tiap
    or if you wanted it all upper case
    Code:
    >>> print a.upper()
    TIAP
    Some of these methods have been listed as deprecated in the python docs, which I don't study enough to know why. Could someone say where to look for the new methods, I understand that things like string.atof() became float() which makes sense to me. However I don't understand how to replace some of the others listed at http://docs.python.org/lib/node42.html. In this post I use string.upper(), what would be correct?

    Comment

    Working...