[Tutor] capitalize() but only first letter

Erik Price eprice@ptc.com
Wed Feb 5 18:12:22 2003


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