Automatic code indentation skipping for triple quoted strings

In short: it looks so ugly to have to remove the identation from the code if we have to generate large strings with triple-quoted strings. Then its harder to read at a glance.

However, if we now change how strings are interpreted from one version to another it could affect a lot of code in updates. Then I imagine creating a tagged’‘’‘string’‘’’ that cleans up those code tabs internally.

Thanks 4 all!

I’m moving this to “Ideas”, as there is no associated PEP.

1 Like

An example would be very helpful.

1 Like

This has come up a few times. Here’s a recent discussion: Str.dedent vs str.removeindent - #25 by methane Short summary: This shouldn’t need to be a tagged string literal form, but as a method instead.

And I agree, this would be a useful method.

1 Like

Have you tried textwrap.dedent? Is this what you mean?


def test():
    # end first line with \ to avoid the empty line!
    s = '''\
    hello
      world
    '''
    print(repr(s))          # prints '    hello\n      world\n    '
    print(repr(dedent(s)))  # prints 'hello\n  world\n'
1 Like

What I’d want is more like the (rather hidden) inspect.cleandoc, which looks for common indentation starting on the second line. This allows the first line to start right after the open quote, so you don’t need to add a blank line just to get the indent fixed.

Not quite sure what your use case is, but I sometimes use the following trick instead of importing textwrap:

code = """if 1:
    foo()
    if bar:
        baz(x)
"""

Hello All! Thank You for your replies!

I mean, if i try to execute this:

class ImportTicket:
    def __init__(self, ticket):
        self.ticket = ticket

    def description(self):
        return f'''
        | | |
        | ------------------ | ------- |
        | **Ticket Id** | {self.ticket["ticket_id"]} |
        | **Subject** | {self.ticket["subject_id"]} |
        '''

    def another_field(self, ticket):
        return int(ticket['another_field'])


def main():
    print(ImportTicket({'ticket_id': 999, 'subject_id': 'problems'}).description())


if __name__ == '__main__':
    main()

the returned string will have the code indent in the literal string, then i need to do this ugly thing for the code works as expected and markdown language will correctly interpretated.

class ImportTicket:
    def __init__(self, ticket):
        self.ticket = ticket

    def description(self):
        return f'''
| | |
| ------------------ | ------- |
| **Ticket Id** | {self.ticket["ticket_id"]} |
| **Subject** | {self.ticket["subject_id"]} |
'''

    def another_field(self, ticket):
        return int(ticket['another_field'])


def main():
    print(ImportTicket({'ticket_id': 999, 'subject_id': 'problems'}).description())


if __name__ == '__main__':
    main()

and i mean… yeah! i can use a function in a literal that literally i just declared for doing my code more readable… but… ¿why?

I think, its not about the code functionality but about code inspection. Moreover… it affects the functionality if a change is done.

For that particular example/use case, I’d simply use textwrap.dedent.