convert ints in a range to strings

Marc Boeren M.Boeren at guidance.nl
Thu Sep 18 09:27:50 EDT 2003


> outputFile = file('ips.txt', 'w')
> r = range(256)
> for r in range(256):
>     f = '192.168.1.%s\n' % r #Change this line to macth your network.
>     outputFile.write(f)
> outputFile.close()

I came up with this:

>>> f = ['192.168.1.%d' % r for r in range(256)]
>>> outputFile = file('ips.txt', 'w')
>>> outputFile.write("\n".join(f))
>>> outputFile.close()

Any comments on which is more pythonic?

Cheerio, Marc.





More information about the Python-list mailing list