removing spaces when mixing variables / text

T

tgiles

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
 
B

Ben Finney

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:
... 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/>
 
M

Mike C. Fletcher

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/
 
A

Amy G

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
 
D

Dang Griffith

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
 
P

Paul Rubin

tgiles said:
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)
 

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,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top