By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,713 Members | 764 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,713 IT Pros & Developers. It's quick & easy.

removing spaces when mixing variables / text

P: n/a
Hi, all.

I'm YAPN (Yet Another Python Newbie), started messing with it last night
and so far, so good. Documentation exellent and the people seem friendly
enough ;)

Ok, I started playing around with random() and decided to write a script
to generate random dotted quad IP addresses, just to see if I could do it.

Everything is perfect with the glaring exeception of the output. It seems
as if when one uses the "," in a print statement, you get an empty space
in between variables and what I want there (namely, the ".").

I've tried it several different ways with no go. Here's the script in all
her cheesy glory:

import random
n = 2
while n > 0:
a = random.randint(1,254)
b = random.randint(1,254)
c = random.randint(1,254)
d = random.randint(1,254)
print a,".",b,".",c,".",d
n = n-1

The output at the moment looks like so:

179 . 72 . 138 . 272
21 . 124 . 83 . 9

When, in a perfect universe it would look like so:

179.72.138.272
21.124.83.9

Thoughts on what critically simple something I missed?

Thanks for the input!

tom
Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Tue, 03 Feb 2004 02:14:18 -0600, tgiles wrote:
import random
n = 2
while n > 0:
a = random.randint(1,254)
b = random.randint(1,254)
c = random.randint(1,254)
d = random.randint(1,254)
print a,".",b,".",c,".",d
n = n-1

The output at the moment looks like so:

179 . 72 . 138 . 272
21 . 124 . 83 . 9


This looks like a good opportunity to learn about the string formatting
operator:
import random
for n in ( 1, 2 ): ... a = random.randint( 1, 254 )
... b = random.randint( 1, 254 )
... c = random.randint( 1, 254 )
... d = random.randint( 1, 254 )
... print "%d.%d.%d.%d" % ( a, b, c, d )
...
147.23.187.25
138.248.253.1


You can learn about many useful Python features like this by working all
the way through the Python tutorial:

<http://www.python.org/doc/tut/>

--
\ "Either he's dead or my watch has stopped." -- Groucho Marx |
`\ |
_o__) |
Ben Finney <http://bignose.squidly.org/>
Jul 18 '05 #2

P: n/a
try this instead

while n > 0:
a = random.randint(1,254)
b = random.randint(1,254)
c = random.randint(1,254)
d = random.randint(1,254)
print str(a) + "." + str(b) + "." + str(c) + "." + str(d)
n -= 1
"tgiles" <tg****@spam.kc.rr.com> wrote in message
news:pa****************************@spam.kc.rr.com ...
Hi, all.

I'm YAPN (Yet Another Python Newbie), started messing with it last night
and so far, so good. Documentation exellent and the people seem friendly
enough ;)

Ok, I started playing around with random() and decided to write a script
to generate random dotted quad IP addresses, just to see if I could do it.

Everything is perfect with the glaring exeception of the output. It seems
as if when one uses the "," in a print statement, you get an empty space
in between variables and what I want there (namely, the ".").

I've tried it several different ways with no go. Here's the script in all
her cheesy glory:

import random
n = 2
while n > 0:
a = random.randint(1,254)
b = random.randint(1,254)
c = random.randint(1,254)
d = random.randint(1,254)
print a,".",b,".",c,".",d
n = n-1

The output at the moment looks like so:

179 . 72 . 138 . 272
21 . 124 . 83 . 9

When, in a perfect universe it would look like so:

179.72.138.272
21.124.83.9

Thoughts on what critically simple something I missed?

Thanks for the input!

tom

Jul 18 '05 #3

P: n/a
tgiles wrote:
....
print a,".",b,".",c,".",d
n = n-1

The output at the moment looks like so:

179 . 72 . 138 . 272
21 . 124 . 83 . 9

When, in a perfect universe it would look like so:

179.72.138.272
21.124.83.9

print is a convenience used to do quick-and-dirty output. It's focused
on that convenience rather than on pretty formatting of reports. So, if
you're into the Java way of doing things:

import sys
sys.stdout.write( repr(a))
sys.stdout.write( '.' )
sys.stdout.write( repr(b))
....

Or, you could use one of these common idioms:

# construct a string with the entire representation before printing
print ".".join( [ str(x) for x in (a,b,c,d) ] )
# or
print str(a)+'.'+str(b)+'.'+str(c)+'.'+str(d)
# or
print '%(a)s.%(b)s.%(c)s.%(d)s'%locals()
# or
print '%s.%s.%s.%s' % (a,b,c,d)

String formatting (seen in those last two examples) is a very powerful
mechanism, more focused on less regular formatting tasks, while the
first example is more appropriate for tasks processing very large
quantities of regular data. The simple addition of strings is fine for
very simple code, but tends to get a little clumsy eventually.

Welcome to the perfect universe :) ,
Mike

_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/

Jul 18 '05 #4

P: n/a
On Tue, 03 Feb 2004 02:14:18 -0600, "tgiles" <tg****@spam.kc.rr.com>
wrote:
Ok, I started playing around with random() and decided to write a script
to generate random dotted quad IP addresses, just to see if I could do it.

Everything is perfect with the glaring exeception of the output. It seems
as if when one uses the "," in a print statement, you get an empty space
in between variables and what I want there (namely, the ".").

Mike pointed out one a common idioms that includes a couple of idioms.
To customize it for random IP addresses:

'.'.join([str(random.randint(0,255)) for i in range(4)])

The two idioms are using the string join method to build a single
string from small parts, connecting with a single value.

The other idiom is a list comprehension. In a list comprehension, you
combine an expression and a loop, sometimes with a condition. In this
example, the expression str(random.randint(0,255)) is independent of
the loop itself. The loop is used to iterate 4 times. I suppose
using range(4) to iterate 4 times could be considered an idiom also.

--dang
Jul 18 '05 #5

P: n/a
"tgiles" <tg****@spam.kc.rr.com> writes:
Everything is perfect with the glaring exeception of the output. It seems
as if when one uses the "," in a print statement, you get an empty space
in between variables and what I want there (namely, the ".").
Yeah, that's because of the somewhat un-Pythonic behavior of the print
statement, which inserts spaces around the printed items, and a newline
at the end.
print a,".",b,".",c,".",d


Try
print "%d.%d.%d.%d." % (a, b, c, d)
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.