change the first character of the line to uppercase in a text file

D'Arcy J.M. Cain darcy at druid.net
Sat Jun 27 16:05:48 CEST 2009


On Sat, 27 Jun 2009 11:54:43 +0100
Angus Rodgers <twirlip at bigfoot.com> wrote:
> On Sat, 27 Jun 2009 11:39:28 +0100, I asked rhetorically:
> >>f = open('test', 'r')
> >>for line in f:
> >>    print line[0].upper()+line[1:],
> >
> >Will your program handle empty lines of input correctly?
> 
> Strangely enough, it seems to do so, but why?

The clue is the comma at the end of the print statement.  It is there
because no lines are empty.  They have at least a newline.

-- 
D'Arcy J.M. Cain <darcy at druid.net>         |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.



More information about the Python-list mailing list