def capture(func):
    """Return the printed output of func().

    `func` should be a function without arguments that produces output with
    print statements.

    >>> def foo():
    ...     print('hello world!')
    ...
    >>> 'hello' in capture(foo)  # foo, not foo()
    True
    >>> capture(lambda: pprint(2/x, use_unicode=False))
    '2\\n-\\nx\\n'

    """
    from io import StringIO
    import sys
    stdout = sys.stdout
    sys.stdout = file = StringIO()
    try:
        func()
    finally:
        sys.stdout = stdout
    return file.getvalue()
