convert ints in a range to strings

Heather Coppersmith me at privacy.net
Wed Sep 17 19:51:28 EDT 2003


On Wed, 17 Sep 2003 19:02:40 -0400,
hokieghal99 <hokiegal99 at hotmail.com> wrote:

> 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

Try any or all of these:

    print a + b + c + str( r )

    print a + b + c + "%d" % r

    print a + b + c + "%s" % r

    print "%s%s%s%d" % (a, b, c, r)

Regards,
Heather

-- 
Heather Coppersmith
That's not right; that's not even wrong. -- Wolfgang Pauli




More information about the Python-list mailing list