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.
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.
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'
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.
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.