strip_ANSI_pat = re.compile(
    """
    \\x1b     
    \\[       
    [;\\d]*   
    [A-Za-z] 
    """,
    re.VERBOSE).sub

def strip_ANSI(s):
    return strip_ANSI_pat('', s)
s = 'potato\x1b[01;32mpotato\x1b[0;0mpotato'
print(s, len(s))
s1 = strip_ANSI(s)
print(s1, len(s1))
Printed length of a string in python