3 Replies - 314 Views - Last Post: 20 January 2013 - 10:47 AM Rate Topic: -----

#1 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 284
  • Joined: 22-July 09

Not understanding some code

Posted 20 January 2013 - 08:05 AM

I was trying to come up with a random collection of characters and upon searching to see if such a list already existed I found this link:
http://stackoverflow...ters-and-digits

Which is all fine and good but when I tried it I set it up a little different. I set it up as such:
import random, string

def idgen(size=8, chars=string.ascii_letters + string.digits):
    newpass = ''
    for x in range(size):
        newpass.join(random.choice(chars))
    return newpass

print(idgen())



My code gives me no output but the one linked does. Am I not understanding something about the code and strings in general?

Is This A Good Question/Topic? 0
  • +

Replies To: Not understanding some code

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2148
  • View blog
  • Posts: 3,297
  • Joined: 21-June 11

Re: Not understanding some code

Posted 20 January 2013 - 08:12 AM

separator.join(lst) will return a string that contains the string separator inserted between each of the items in the list lst. So "-".join(["a", "b", "c"]) would return "a-b-c".

join will not modify the string it's called on in any way (in fact strings are immutable) and calling join without doing anything with the result (like storing it in a variable, printing it or passing it to a function) has no effect whatsoever.

This post has been edited by sepp2k: 20 January 2013 - 08:12 AM

Was This Post Helpful? 0
  • +
  • -

#3 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 284
  • Joined: 22-July 09

Re: Not understanding some code

Posted 20 January 2013 - 09:10 AM

View Postsepp2k, on 20 January 2013 - 10:12 AM, said:

separator.join(lst) will return a string that contains the string separator inserted between each of the items in the list lst. So "-".join(["a", "b", "c"]) would return "a-b-c".

join will not modify the string it's called on in any way (in fact strings are immutable) and calling join without doing anything with the result (like storing it in a variable, printing it or passing it to a function) has no effect whatsoever.

Thank you. Definately helpful.
Was This Post Helpful? 0
  • +
  • -

#4 woooee  Icon User is offline

  • D.I.C Head

Reputation: 25
  • View blog
  • Posts: 100
  • Joined: 21-November 12

Re: Not understanding some code

Posted 20 January 2013 - 10:47 AM

You can also use random.shuffle() when the list is not too long. Note that random.choice() can return the same item more than once.
import random
import string

chars=list(string.ascii_letters + string.digits)
print chars, "\n"
random.shuffle(chars)
print chars[:8] 

This post has been edited by woooee: 20 January 2013 - 10:48 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1