convert ints in a range to strings

H

hokieghal99

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!!!!
 
D

Duncan Smith

hokieghal99 said:
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!!!!

I believe the second approach is generally faster.

Duncan
 
P

Paul Rubin

hokieghal99 said:
------------------------------
a="192."
b="168."
c="1."
r = range(256)
for r in r:
print a + b + c + r

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

Heather Coppersmith

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
 
H

hokiegal99

Thanks Heather... this did it:

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()
 
M

Michael Sweeney

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

You could try this:

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


192.168.1.0
192.168.1.1
192.168.1.2
192.168.1.3
....
192.168.1.255

- Mike
 
A

Asun Friere

hokieghal99 said:
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
 
R

Raymond Hettinger

[hokieghal99]
a="192."
b="168."
c="1."
r = range(256)
for r in r:

This rebinding of r is an amazing non-error.
I had to read it twice before I believed that it worked.
Working or not, don't do this. Changing the meaning
of variable like this drives some people off the edge
and they end up working in pure functional languages
and never recover.
print a + b + c + r

As the others pointed out, str(r) will do the trick.


Raymond Hettinger
 
D

Duncan Smith

hokiegal99 said:
Thanks Heather... this did it:

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()

[snip]

The second line of this code does nothing useful. My feeling is that as r
is an integer you should really use %d, rather than %s.

Duncan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top