[Tutor] Simple PassGen

W W srilyk at gmail.com
Mon Feb 9 22:53:30 CET 2009


On Mon, Feb 9, 2009 at 3:35 PM, Kayvan Sarikhani <ksarikhani at gmail.com>wrote:

> Hello Tutors,
> <snip>  I thought that maybe adding "print random.choice(pool).strip()"
> might work but not having any luck with that. Is the output this way, simply
> because of the nature of the range, or can anyone point my in the right
> direction? Thanks in advance!
>

It's actually not the range but the print function. Print automatically
prints a newline. If you were to create a string of what print does:

print 'foo'

'foo\n'

However, adding a trailing comma will eliminate that. It will still add a
trailing space however. A better option would be like Marc suggests,
although you have to do concatenation:

mystr += random.choice(pool)

You do have some other options though. Create a list and join it with '':

In [148]: x = ['a','b','c']

In [149]: ''.join(x)
Out[149]: 'abc'

Create a list, convert it to tuple, and use string formatting:

In [151]: '%s%s%s' % t
Out[151]: 'abc'

Use sys.stdout.write instead:

In [154]: for y in x:
   .....:     sys.stdout.write(y)
   .....:
   .....:
abc

and a host of other options. The simplest is probably the concatenation
though ;)
HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090209/46361895/attachment.htm>


More information about the Tutor mailing list