skip to Main Content

So here is an example of a multiline string in vscode/python:

enter image description here

Cursor is after the p , and then you press enter, and end up like this:

enter image description here

i.e. the string ends up indented, which seems what you almost never want – why have an arbitratly amount of whitespace on the next line of this string ?

Is there any way change this in vscode, i.e. for multiline strings, it should end up with this:

enter image description here

2

Answers


  1. I think this problem is related to different coding styles of different people.

    For example,

    def example(x):
        if x:
            a = '''
    This is help
    '''
    
    def example(x):
        if x:
            a = '''This is help
    '''
    

    The automatic indenting of vscode line breaks is based on code blocks. If you want Vscode can identify multiline string, I think it would be better to submit future request in github. I’ve submitted this issue for you.

    Login or Signup to reply.
  2. I am not 100% sure if what OP meant is just to refer to the indentation in the editor (namely, VSC) or if, by this:

    i.e. the string ends up indented, which seems what you almost never want – why have an arbitrary amount of white space on the next line of this string?

    …they also meant to refer to the actual output of the multi-line string,
    (or also, just in case anybody else finds this post looking for a way to avoid this affecting the actual output of the multi-line string), I’d like to add as a complementary answer (cannot comment yet) that this was already beautifully answered here.

    If that’s the case and you’re reading this for that reason, in short, all you want is to import the standard lib ‘inspect’ and post-process your string with it, using the cleandoc method.

    Without breaking the indentation in your IDE, this method makes sure to give you the string output you actually expected:

    All leading whitespace is removed from the first line. Any leading whitespace that can be uniformly removed from the second line onwards is removed. Empty lines at the beginning and end are subsequently removed. Also, all tabs are expanded to spaces.
    (From the docs link above)

    Hope that helps anyone.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search