change the first character of the line to uppercase in a text file
__peter__ at web.de
Sat Jun 27 07:02:47 EDT 2009
Angus Rodgers wrote:
> On Sat, 27 Jun 2009 11:39:28 +0100, I asked rhetorically:
>>On Fri, 26 Jun 2009 18:58:27 -0700 (PDT), powah
>><wong_powah at yahoo.ca> wrote:
>>>Thank you for your hint.
>>>This is my solution:
>>>f = open('test', 'r')
>>>for line in f:
>>> print line.upper()+line[1:],
>>Will your program handle empty lines of input correctly?
> Strangely enough, it seems to do so, but why?
Because there aren't any. When you read lines from a file there will always
be at least the newline character. Otherwise it would indeed fail:
>>> for line in "peter\npaul\n\nmary".splitlines():
... print line.upper() + line[1:]
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
IndexError: string index out of range
More information about the Python-list