[Tutor] Append mode dilemma

Luke Paireepinart rabidpoobear at gmail.com
Wed Dec 9 19:58:31 CET 2009


On Wed, Dec 9, 2009 at 12:49 PM, biboy mendz <bibsmendez at gmail.com> wrote:

>
>> Are you aware of how 'join' works?
>>
> Hi Luke,
>
> Thank you. To be honest I'm confused of the different string methods like
> join(), split(), etc. Anyway I will practice them to see how they work.
>
>
> It's very important to practice these string methods, you should get
intimately acquainted with all the builtin objects and functions, they're
extremely flexible and usually very fast as well.

The main problem with your 'join' is that join takes an interable and
creates a new string with the LHS argument in between each iterated item.
So if you join 'a' 'b' 'c', with # you will get 'a#b#c'.
Note that there is no # at the beginning or end.

In your case, you are outputting to a file, and you want every file to end
with a newline, so you simply need to append another newline to the end.
"#".join(['a','b','c']) + "#" would do the trick for the # example.  The
result would be "a#b#c#".

Just to be clear, make sure you have your extra newline at the end, not the
beginning of the print.  Otherwise your first line in the file will be
blank.  So it does matter where you put the newline.


HTH,
-Luke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091209/70b1b445/attachment-0001.htm>


More information about the Tutor mailing list