Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 3rd, 2012  |  syntax: None  |  size: 0.37 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. def random_string(n_chars):
  2.     '''Generates a random string of length n_chars.
  3.  
  4.     >>> random_string(5)
  5.     'FM>ro'
  6.     >>> random_string(8)
  7.     '"K^V]J|/'
  8.     >>> random_string(32)
  9.     ')tTu,2"s`ke`MF0}qWd.-&__C8OEksQy'
  10.  
  11.     :param n_chars: Number of characters to pick
  12.     :type n_chars: int
  13.  
  14.     '''
  15.  
  16.     return ''.join( chr(random.randrange(32, 126)) for i in xrange(n_chars) )