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

powah wong_powah at yahoo.ca
Sat Jun 27 03:58:27 CEST 2009


On Jun 26, 4:51 pm, Chris Rebert <c... at rebertia.com> wrote:
> On Fri, Jun 26, 2009 at 12:43 PM, powah<wong_po... at yahoo.ca> wrote:
> > How to change the first character of the line to uppercase in a text
> > file?
> > e.g.
> > input is:
> > abc xyz
> > Bd ef
> > gH ij
>
> > output should be:
> > Abc xyz
> > Bd ef
> > GH ij
>
> We're not in the business of doing homework. Some hints though:
>
> `s.upper()` converts the string in variable `s` to all upper case
> (e.g. "aBcD".upper() --> "ABCD")
> `for line in afile:` iterates over each line in a file object. `afile`
> is the file object and `line` gets assigned each line in turn.
> `s[x]` gets you the (x+1)-th character in the string `s` (e.g.
> "abcd"[2] --> "c")
>
> And here are the docs on working with files:http://docs.python.org/library/functions.html#openhttp://docs.python.org/library/stdtypes.html#file-objects
>
> That should be enough to get you started.
>
> Cheers,
> Chris
> --http://blog.rebertia.com

Thank you for your hint.
This is my solution:
f = open('test', 'r')
for line in f:
    print line[0].upper()+line[1:],



More information about the Python-list mailing list