code: def rand_str(allowed, length):
      
      if (not isinstance(length, int)) or (length <= 0):
          raise Exception("length parameter has to be an integer greater than 0!")
      
      res = list()
      for _ in range(length):
          res.append(random.choice(allowed))
      return "".join(res)
docs: Generate fixed-length random string from your allowed character pool.
      
      Usage Example::
      
          >>> import string
          >>> randstr(string.ascii_letters + string.digits, 32)
          QE1UKzSrIw8z7EiEp94FJ3jwmuYNUw2N