[Tutor] capitalize() but only first letter
Alfred Milgrom
fredm@smartypantsco.com
Wed Feb 5 18:33:01 2003
The simple way to slice a string is to use the string[start:end] construct.
The start and end parameters are optional (but remember we start at 0), so
you can have:
s = 'everything But The kitchen siNk'
s[3:7] means slice from 4th to 8th letter (inclusive)
'ryth'
s[:-1] means everything from the beginning except the last letter
'everything But The kitchen siN'
s[1:] means everything except the first letter
'verything But The kitchen siNk'
HTH
Fred Milgrom
At 06:05 PM 5/02/03 -0500, Erik Price wrote:
>Erik Price wrote:
>>I'm writing a Python script that generates a JavaBean getter method and
>>setter method based on command line parameters (type and property
>>name). It works beautiful, except for one thing. I'm using the
>>capitalize() method of the Python String type, and although this does
>>capitalize the first letter of the word, it lowercases all the rest of
>>the letters. I need to capitalize just the first letter and leave all
>>the other letters in the word alone.
>>(1) Where can I read the source of the String type? I'm using Python
>>2.2.2 on Cygwin.
>>(2) What would be the best way to extract the first character from a
>>string variable?
>
>Actually, I posted too soon -- I figured it out. Strings are lists of
>characters, apparently. But what is the elegant way of declaring a slice
>of s[n] to the end of s, so I can capitalize the first character and
>prepend it to the rest?
>
>s = "string"
>s = s[0].capitalize() + s[1] (and all the rest of the chars too)
>
>
>Erik
>
>
>_______________________________________________
>Tutor maillist - Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor