
from random import choice
import string

def random_string(length,
        letters=True,
        digits=True,
        punctuation=False,
        whitespace=False):
    """ Returns a random string """
    chars = ''
    if letters:
        chars += string.ascii_letters
    if digits:
        chars += string.digits
    if punctuation:
        chars += string.punctuation
    if whitespace:
        chars += string.whitespace
    return ''.join([choice(chars) for i in range(length)])


import random, string

def random_string(length,
        letters=True,
        digits=True,
        punctuation=False,
        whitespace=False):
    """ Returns a random string """
    chars = ''
    if letters:
        chars += string.ascii_letters
    if digits:
        chars += string.digits
    if punctuation:
        chars += string.punctuation
    if whitespace:
        chars += string.whitespace
    return ''.join(random.sample(chars, length))

Use random.sample() rather than reinventing it.
Use random.sample() rather than reinventing it.

  
- from random import choice
- import string
+ import random, string
  
  def random_string(length,
          letters=True,
          digits=True,
          punctuation=False,
          whitespace=False):
      """ Returns a random string """
      chars = ''
      if letters:
          chars += string.ascii_letters
      if digits:
          chars += string.digits
      if punctuation:
          chars += string.punctuation
      if whitespace:
          chars += string.whitespace
-     return ''.join([choice(chars) for i in range(length)])
+     return ''.join(random.sample(chars, length))
  
Use random.sample() rather than reinventing it.
## Code Before:

from random import choice
import string

def random_string(length,
        letters=True,
        digits=True,
        punctuation=False,
        whitespace=False):
    """ Returns a random string """
    chars = ''
    if letters:
        chars += string.ascii_letters
    if digits:
        chars += string.digits
    if punctuation:
        chars += string.punctuation
    if whitespace:
        chars += string.whitespace
    return ''.join([choice(chars) for i in range(length)])

## Instruction:
Use random.sample() rather than reinventing it.
## Code After:

import random, string

def random_string(length,
        letters=True,
        digits=True,
        punctuation=False,
        whitespace=False):
    """ Returns a random string """
    chars = ''
    if letters:
        chars += string.ascii_letters
    if digits:
        chars += string.digits
    if punctuation:
        chars += string.punctuation
    if whitespace:
        chars += string.whitespace
    return ''.join(random.sample(chars, length))
