Skip to main content
added 66 characters in body
Source Link
Totem
  • 7.3k
  • 5
  • 40
  • 66

This gives control over the length of the generated string, however, as @Keven mentioned in the comments, if you want less chance of collision in the future, you need to use more characters. Though it seems quite unlikely.

from random import sample
from string import digits, ascii_letters 

chars = digits + ascii_letters
#'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

gen_str = sample(chars, 10)

example output:

['2', '8', 'G', 'o', 'U', 'e', 'K', 'f', 'w', '7']

>>> ''.join(gen_str)

'28GoUeKfw7'

info on random module

This gives control over the length of the generated string, however, as @Keven mentioned in the comments, if you want less chance of collision in the future, you need to use more characters

from random import sample
from string import digits, ascii_letters

chars = digits + ascii_letters

gen_str = sample(chars, 10)

example output:

['2', '8', 'G', 'o', 'U', 'e', 'K', 'f', 'w', '7']

>>> ''.join(gen_str)

'28GoUeKfw7'

info on random module

This gives control over the length of the generated string, however, as @Keven mentioned in the comments, if you want less chance of collision in the future, you need to use more characters. Though it seems quite unlikely.

from random import sample
from string import digits, ascii_letters 

chars = digits + ascii_letters
#'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

gen_str = sample(chars, 10)

example output:

['2', '8', 'G', 'o', 'U', 'e', 'K', 'f', 'w', '7']

>>> ''.join(gen_str)

'28GoUeKfw7'

info on random module

Source Link
Totem
  • 7.3k
  • 5
  • 40
  • 66

This gives control over the length of the generated string, however, as @Keven mentioned in the comments, if you want less chance of collision in the future, you need to use more characters

from random import sample
from string import digits, ascii_letters

chars = digits + ascii_letters

gen_str = sample(chars, 10)

example output:

['2', '8', 'G', 'o', 'U', 'e', 'K', 'f', 'w', '7']

>>> ''.join(gen_str)

'28GoUeKfw7'

info on random module