import sys
from io import StringIO
from contextlib import contextmanager

@contextmanager
def print_interceptor(buf):
	real_stdout = sys.stdout
	sys.stdout = buf
	yield
	sys.stdout = real_stdout
	
buf = StringIO()
with print_interceptor(buf):
	print("Hello, world!")
	
print("Intercepted: " + repr(buf.getvalue()))