removing spaces when mixing variables / text

Ben Finney bignose-hates-spam at and-benfinney-does-too.id.au
Tue Feb 3 03:22:57 EST 2004


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



More information about the Python-list mailing list