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

    Join Date
    Jan 2004
    Location
    Atlantic City, NJ
    Posts
    327
    Rep Power
    13

    String List Manipulation


    Well just when I thought I was beginning to learn this stuff I hit another wall. I'm trying to convert a string to another string with everything else in uppercase except for the first word. I've gotten this far:

    Code:
    string = "This is the string"
    new_string = string.split(' ')
    Now I have a list like this:

    Code:
    ['This', 'is', 'the', 'string']
    So, I want to uppercase "is the string" and put it back into one string that would look like this:

    Code:
    "This IS THE STRING"
    This seems so easy but I can't figure out how to do it.
    I'll learn this stuff someday.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    174
    Rep Power
    11

    sf2k


    you're so close! please take another look at the split function as you can add additional parameters.

    Currently, string.split('') will take each and every space, chop it, and put it into your list. But do you want each and every word chopped up? Ah ha! no. I think just the first word eh?

    so since the split function automatically splits on space anyway, we can just ask it to do it on the first word only:

    (I forgot, but I think it's zero indexed.. anyway you'll get the idea! Splits firstword into "firstword" and rest of string in "rest" oddly enough)
    Code:
    mystring = "This is the string"
    firstword, rest = mystring.split(0,None)
    Now you can make the "rest" uppercase and then meld it back into a merged string

    Code:
    finalstring = firstword + ' ' + string.capwords(rest)
    give it a try

    cheers
    sf2k

    (btw if this was useful, click on the scales icon for this post at top right...thanks)

    Comments on this post

    • AtomJay disagrees
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Here's an example of how to use the split() method to only split the string on the first space.

    Code:
    >>> aString = "This is the string"
    >>> 
    >>> splitString = aString.split(' ', 1)
    >>> splitString
    ['This', 'is the string']
    >>>
    >>> '%s %s' % (splitString[0], splitString[1].upper())
    'This IS THE STRING'
    >>> splitString[0] + ' ' + splitString[1].upper() 
    'This IS THE STRING'
    >>> print splitString[0], splitString[1].upper()
    This IS THE STRING
    >>>
    As you can see, its really very simple. The first argument to the split() method is the character you want to split the string on and the second is the number of times you want to split the string.

    Note: this starts from the left and moves to the right. If you want to do this in reverse then you can use the rsplit() method in the same way.

    If you want to alter the case of your string you should use the upper(), lower, title(), swapcase() and capitalize() string methods rather than using the string module however this is still useful for some things so don't discard it yet .

    http://www.python.org/doc/2.4/lib/module-string.html

    Note: it is not generally a good idea to use the same name as one of the standard modules however if you are not using the module in your program everything should be fine.

    To illustrate the difference between the lines that construct a new string and the line that prints the string in the example above I've continued here, however this is really just a follow up.

    Code:
    >>> string1 = '%s %s' % (splitString[0], splitString[1].upper())
    >>> string1
    'This IS THE STRING'
    >>> string2 = splitString[0] + ' ' + splitString[1].upper()
    >>> string2
    'This IS THE STRING'
    >>> 
    >>> string3 = splitString[0], splitString[1].upper()
    >>> string3
    ('This', 'IS THE STRING')
    >>>
    The first two can be assigned to a variable in the same way, but if you try to do the same using commas line in the print statement you'll get something you might not have been expecting: a Tuple. Keep this in mind .

    Hope this helps,

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

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Atlantic City, NJ
    Posts
    327
    Rep Power
    13
    Thanks guys.

    I knew it was right there but I had been looking at code all day and couldn't figure it out.

    Code:
    splitString[0] + ' ' + splitString[1].upper()
    Thats the ticket right there. Oh yes, I don't use 'string' as a name but I did in that example. Thanks again.
    I'll learn this stuff someday.

IMN logo majestic logo threadwatch logo seochat tools logo