1

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

binary01
  • 1,728
  • 2
  • 13
  • 27
  • You should use normal strings, not docstrings, for variable values. – 101 Sep 30 '22 at 02:14
  • @101 There certainly are valid reasons to have *long* strings, like the beginning of a 2-3 paragraph string I started in the post. Strings so long you should not reasonably put them on a single line, but still a string that's not a docstring That's what pythons multiline strings are for, they're not only suitable for docstrings. – binary01 Oct 07 '22 at 12:46

2 Answers2

0

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.

MingJie-MSFT
  • 5,569
  • 1
  • 2
  • 13
  • Nice. I'm not sure this is related to coding styles though, it's more about multiline strings, and in either of your two examples you would want to start writing at the beginning of the next line - throwing in a large number of whitespaces just because that's the block indent where the variable is declared would be wrong in both cases. (and of course for a short string like this, multiline strings doesn't make sense - however it was the start of a longer 3 paragraph string) – binary01 Oct 07 '22 at 12:51
0

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.