convert ints in a range to strings

Duncan Smith buzzard at urubu.freeserve.co.uk
Wed Sep 17 19:14:07 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!!!!
>
>
>
>

>>> a="192."
>>> b="168."
>>> c="1."
>>> r = 12
>>> a + b + c + str(r)
'192.168.1.12'
>>> '%s%s%s%d' % (a,b,c,r)
'192.168.1.12'
>>>

I believe the second approach is generally faster.

Duncan






More information about the Python-list mailing list