removing spaces when mixing variables / text

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tgiles

    removing spaces when mixing variables / text

    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
  • Ben Finney

    #2
    Re: removing spaces when mixing variables / text

    On Tue, 03 Feb 2004 02:14:18 -0600, tgiles wrote:[color=blue]
    > 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[/color]

    This looks like a good opportunity to learn about the string formatting
    operator:
    [color=blue][color=green][color=darkred]
    >>> import random
    >>> for n in ( 1, 2 ):[/color][/color][/color]
    ... 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[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

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

    Comment

    • Amy G

      #3
      Re: removing spaces when mixing variables / text

      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" <tgiles@spam.kc .rr.com> wrote in message
      news:pan.2004.0 2.03.08.14.18.5 42560@spam.kc.r r.com...[color=blue]
      > 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[/color]


      Comment

      • Mike C. Fletcher

        #4
        Re: removing spaces when mixing variables / text

        tgiles wrote:
        ....
        [color=blue]
        > 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.2 72
        >21.124.83.9
        >
        >[/color]
        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.writ e( repr(a))
        sys.stdout.writ e( '.' )
        sys.stdout.writ e( 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'%loca ls()
        # 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




        Comment

        • Dang Griffith

          #5
          Re: removing spaces when mixing variables / text

          On Tue, 03 Feb 2004 02:14:18 -0600, "tgiles" <tgiles@spam.kc .rr.com>
          wrote:
          [color=blue]
          >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 ".").[/color]
          Mike pointed out one a common idioms that includes a couple of idioms.
          To customize it for random IP addresses:

          '.'.join([str(random.rand int(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.rand int(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

          Comment

          • Paul Rubin

            #6
            Re: removing spaces when mixing variables / text

            "tgiles" <tgiles@spam.kc .rr.com> writes:[color=blue]
            > 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 ".").[/color]

            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.
            [color=blue]
            > print a,".",b,".",c," .",d[/color]

            Try
            print "%d.%d.%d.% d." % (a, b, c, d)

            Comment

            Working...