classification
Title: Enhancement: i-Strings
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.9
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: Aditya Shankar, SilentGhost, xtreak
Priority: normal Keywords:

Created on 2019-04-14 13:58 by Aditya Shankar, last changed 2019-04-15 06:26 by Aditya Shankar. This issue is now closed.

Messages (3)
msg340208 - (view) Author: Aditya Shankar (Aditya Shankar) Date: 2019-04-14 13:58
Problem: multiline strings are a pain to represent (other than of-course in docstrings), representing a multiline string inside a function looks something like this -

def foo():
    # some code
    ...
    ...
    # some code
    text = """abc meta alpha chronos
dudes uptomes this text
is nonsense"""
    return somethingwith(text)

or

def foo():
    # some code
    ...
    ...
    # some code
    text = "\n".join(["abc meta alpha chronos",
                      "dudes uptomes this text",
                      "is nonsense"])
    return somethingwith(text)

an enhancement would be - 

def foo():
    # some code
    ...
    ...
    # some code
    text = i"""
            abc meta alpha chronos
            dudes uptomes this text
            is nonsense
            """
    return somethingwith(text)
i.e. all initial spaces are not considered as a part of the string in each ine

for example while throwing an exception -
def foo(bad_param):
    ...
    try:
        some_function_on(bad_param)
    except someException:
        throw(fi"""
                you cant do that because, and I'm gonna explain
                this in a paragraph of text with this {variable}
                because it explains things more clearly, also
                here is the {bad_param}
                """)
    ...
which is far neater than -

def foo(bad_param):
    ...
    try:
        some_function_on(bad_param)
    except someException:
        throw(f"""you cant do that because, and I'm gonna explain
this in a paragraph of text with this {variable}
because it explains things more clearly, also
here is the {bad_param}""")
    ...

pros:
    - represented code is closer to output text
    - implementation should not be too hard
msg340210 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2019-04-14 14:10
This type of enhancements should be in the first place discussed on python-ideas mailing list and a PEP would probably be needed at the second stage. Not that I think it's likely this suggestion has much chance.

What you're desiring can already be implemented using implicit string concatenation (with addition of some parentheses in some of your examples).
msg340213 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-04-14 14:42
Some discussions in the past and the common suggestion/idiom is to use textwrap.dedent. I agree with @SilentGhost that it should be first brought up in python-ideas and also addressing the concerns for similar proposal in the past if they are still valid.

Proposes d"" for indentation : https://mail.python.org/pipermail/python-ideas/2010-November/008589.html
https://mail.python.org/pipermail/python-dev/2005-July/054649.html
History
Date User Action Args
2019-04-15 06:26:08Aditya Shankarsetresolution: postponed -> duplicate
2019-04-14 14:42:48xtreaksetnosy: + xtreak
messages: + msg340213
2019-04-14 14:10:05SilentGhostsetstatus: open -> closed

nosy: + SilentGhost
messages: + msg340210

resolution: postponed
stage: resolved
2019-04-14 13:58:27Aditya Shankarcreate