Hello all. I am very excited to have found this place in that I might better my very shoddy coding skills. I am trying to learn Python with a couple of books and some ideas for programs. I am trying to figure out a matter of what I think is an output formatting issue.

file=open('c:\\Users\\Minus\\Desktop\\PyTest.txt', 'w')

for i in range(0, 105, 5):
    print(i, end=" ")

What I am trying to accomplish, is to output the result of the code to a text file. While this code will output the integers within my range, it does it like this 05101520253035404550556065707580859095100

I am simply trying to make the output of the code cleaner by inserting a space between integers. I am pretty convinced that my code is overkill for what I am trying to do and that it is a simple fix. I have searched for a solution, but have not found what I think is the right answer and am hoping someone here can give me a hand.

I have looked at string formatting and thought of making a list and then using replacement fields, but it seems like printing 20 elements is more work than being able to insert a space.

My questions are: can I simply insert a space into the output, if so, how? Is there a better, cleaner way to execute this code?

I am still very green with regard to Python and do appreciate any help.


5 Years
Discussion Span
Last Post by Lardmeister
outstring=' '.join([str(i) for i in range(0,105,5)])

Also, it's a good habit not to use the word "file" as this is a python reserved file. I'd recommend outfile or just o.

Edited by hughesadam_87


Only do not use o or l as variable names, they are too easy to mix with 0 and1


Thanks very much for the help and solution. It will definitely help me to write better code!


Another option ...

with open('PyTest.txt', 'w') as fout:
    for i in range(0, 105, 5):
        fout.write(str(i) + ' ')

'''content of file PyTest.txt ... 
0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 

If you want to use formatting, then do something like this ...

with open('PyTest.txt', 'w') as fout:
    for i in range(0, 105, 5):
        sf = "{} ".format(i)

Edited by vegaseat


Yes with open(...) always close the file object when a iteration has finished.

The advantage of using a with statement is that it is guaranteed to close the file no matter if nested block exits.
If the nested block were to contain a return statement,or a continue or break statement,
the with statement would automatically close the file in those cases too.

In the background with statement dos exception handling(try-finally)
Look like this at the end.


Edited by snippsat


Vegas, I was considering an option like you have suggested but using replacement fields instead of """. I figured that if I were to increase the size of the output say, range(0,1000,5) for example, that would take forever. I do like having that as an option though for smaller examples. I definitely like the second option with the formatting too!


You are right, for a large range hughesadam_87 outstring code would make the most sense.
Writing to disk so many times would be slow.

Edited by Lardmeister

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.