if codepoint <= 65535:
    return 3
if codepoint <= 1114111:
    return 4
raise ValueError('Invalid Unicode character: ' + hex(codepoint))

def utf8_char_len_2(c):
    return len(c.encode('utf-8'))
utf8_char_len = utf8_char_len_1

def utf8len(s):
    return sum(utf8_char_len(c) for c in s)
How can I determine the byte length of a utf-8 encoded string in Python?