I wondered if there was any better way of doing so.I had a hard time figuring it out (it seemed nobody else already tried to do so).import reimport stringclass BetterFormat(string.Formatter):     Introduce better formatting     def parse(self, format_string):         Receive the string to be transformed and split its elements to facilitate the actual replacing         # Automatically counts how many tabs are before the {} then automatically adds the new rule (defined in format_field)        return [(before, identifiant, str(len(re.search('\t*$', before).group(0))) + '\t' + (param if param is not None else ''), modif) for before, identifiant, param, modif in super().parse(format_string)]    def format_field(self, v, pattern):         Receive the string to be transformed and the pattern according which it is supposed to be modified         # Hacky way to remove a dynamic sequence of characters ([0-9]+\t) and returning it        sharedData = {'numberOfTabs': 0}        def extractTabs(pattern):            sharedData['numberOfTabs'] = int(pattern.group(0)[:-1]) # Save the data that will be erased            return  # This will erase it        pattern = re.sub('[0-9]+\t', extractTabs, pattern)        if sharedData['numberOfTabs']: # If there are tabs to be added            v = (sharedData['numberOfTabs'] * '\t').join(v.splitlines(True))        return super().format_field(v, pattern)css = /* Some multi-lines CSSAll automatically indented */print(BetterFormat.format(<html>    <head>        <style type=text/css>            {css}        </style>    </head>    <body>        <!-- Whatever -->    </body></html>,    css=css )) # Output <html>    <head>        <style type=text/css>            /* Some multi-line CSS             All automatically indented */        </style>    </head>    <body>        <!-- Whatever -->    </body></html>Otherwise, previously I had to do this:# []print( []            {css}[].format(css='\t\t\t'.join(css.splitlines(True))))And just so you better understand, I first only implemented BetterFormat.format_field, then I was able to do that, but I was still required to count the number of tabulations myself:# []print( []            {css:3\t}[].format(css=css))    
Automatically indent a whole block of text into str.format
python;strings;python 3.x