convert ints in a range to strings

Asun Friere afriere at yahoo.co.uk
Thu Sep 18 00:35:58 EDT 2003


hokieghal99 <hokiegal99 at hotmail.com> wrote in message news:<bkap6g$4o1$1 at solaris.cc.vt.edu>...
> Hi,
> 
> I'm trying to do this:
> 
> ------------------------------
> a="192."
> b="168."
> c="1."
> r = range(256)
> for r in r:
> 	print a + b + c + r
> ------------------------------
> 
> But, I get this error: cannot concatenate 'str' and 'int' objects. So, I 
> need to convert the ints in the range to strs, but I do not know how to 
> do that. Could someone help me? Ultimately, I want to print out 
> something like this to a text file:
> 
> 192.168.1.0
> 192.168.1.1
> 192.168.1.2
> ...
> 192.168.1.255
> 
> Thanks!!!!

for x in r :
    print a + b + c + str(x)


but why not use ints all the way along like this:

a = 192
b = 168
c = 1
for n in range(256) :
    print "%d.%d.%d.%d" % (a, b, c, n)

or simply don't use them at all:

for n in range(256) :
    print "192.168.1.%d" % n




More information about the Python-list mailing list