I propose to add indented multi-line string literals [similar to triple-quoted string literals in Julia or in Swift] support to Python through I
(or i
) prefix [i
means indented] in order to match the surrounding code.
For example, this code:
print(I"""First line.
Second line.""")
print(RI"""//\\
\\//""")
will be equivalent to:
print("First line.\nSecond line.")
print("//\\\\\n\\\\//")
This is especially useful for strings inside deeply nested functions (inspired by this question):
def method():
string = I"""line one
line two
line three"""
or
def method():
string = \
I"""line one
line two
line three"""
looks much better than
def method():
string = """line one
line two
line three"""
or even
def method():
string = """\
line one
line two
line three"""
and will work faster than using textwrap.dedent()
or something like that.
To avoid ācombinatorial explosionā mentioned here, I think that I
prefix should be the last one, i.e. prefixes such as IR
or If
are not allowed. (Also I
looks like a vertical bar |
, so it makes more sense that quotes follow right behind it.)
P.S. I know that itās not a new idea, but still I think this feature should be added to Python, sooner or later, in some form or another.
- Yes
- Maybe
- No
0 voters