Generate python code to encode a given string in base64.
string = 'This is a test string.'
def base64_encode(string):
    encoded_string = base64.b64encode(string.encode('utf-8')) 
    return encoded_string.decode('utf-8')