473,804 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

convert ints in a range to strings

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


Jul 18 '05 #1
9 3280

"hokieghal9 9" <ho********@hot mail.com> wrote in message
news:bk******** **@solaris.cc.v t.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
Jul 18 '05 #2
hokieghal99 <ho********@hot mail.com> writes:
------------------------------
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
Jul 18 '05 #3
Thank you (Paul & Duncan) for showing me how to do this!

Paul Rubin wrote:
hokieghal99 <ho********@hot mail.com> writes:
------------------------------
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

Jul 18 '05 #4
On Wed, 17 Sep 2003 19:02:40 -0400,
hokieghal99 <ho********@hot mail.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
Jul 18 '05 #5
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.writ e(f)
outputFile.clos e()

Heather Coppersmith wrote:
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

Jul 18 '05 #6
> 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
Jul 18 '05 #7
hokieghal99 <ho********@hot mail.com> wrote in message news:<bk******* ***@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
Jul 18 '05 #8
[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
Jul 18 '05 #9

"hokiegal99 " <ho********@hot mail.com> wrote in message
news:3F******** ******@hotmail. com...
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.writ e(f)
outputFile.clos e()


[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
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2972
by: Klaus Neuner | last post by:
Hello, I need a function that converts a list into a set of regexes. Like so: string_list = print string_list2regexes(string_list) This should return something like:
13
41825
by: Hako | last post by:
I try this command: >>> import string >>> string.atoi('78',16) 120 this is 120 not 4E. Someone can tell me how to convert a decimal number to hex number? Can print A, B, C,DEF. Thank you.
4
9776
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a numeric value according to an arbitrary regular expression.
30
8689
by: John Carroll | last post by:
Does anyone have a function or procedure for converting integers to character strings? Thank you, John
1
5193
by: Marc Lefebvre | last post by:
How can I convert an Int to a BitArray ? Or How can I format an Int to a binairy string ? Thank's Marc Lefebvre
1
2204
by: Andreas Klemt | last post by:
Hello, I have this session("myNumber") = 888 Dim intNumber As Integer a) intNumber = session("myNumber") b) intNumber = Convert.ToInt32(session("myNumber"))
5
3441
by: Allerdyce.John | last post by:
Do I need to convert string to integer in python? or it will do it for me (since dynamic type)? In my python script, I have this line: x /= 10; when i run it, I get this error: TypeError: unsupported operand type(s) for /=: 'unicode' and 'int' I want to divide x by 10 and assign that value back to x.
3
8753
by: mrajanikrishna | last post by:
Hi Friends, I am accepting a number from the user entered in a textbox. I want to assign to a variable in my code and assignt this to that variable. double num1 = (double)txtNum1.text; this produced an error
19
5351
by: est | last post by:
From python manual str( ) Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that str(object) does not always attempt to return a string that is acceptable to eval(); its goal is to return a printable string. If no argument is given, returns the empty string, ''.
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10359
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9177
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6870
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3837
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3005
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.