12
Wisecrack
351d

The infuriating edgecases of python copypasta.

If you're like me, and you find it easier to noodle in notepad++ and the console, then you may have encountered this peculiar bug.

Try padding blank spaces on an empty string variable, and follow it with print(blanks + str(var))
#for any variable

Now copypaste that along with at least one other line at the same time.

Observe how no matter what you do, print will always output the blanks variable on a separate line, with quotes.

Try rewriting right-justify? No good.
Try using f-format strings? No good.
Raw strings? Inspecting bytes to see where the newlines and carriage returns are being inserted? Nothing.

Copypaste with multiple strings will *always* insert quotes and a new line when printing *any* variable with a string thats been justified.
And this is 100% non-congruent with pasting the same *line* of code *by itself*, which works as intended, no quotes or additional new lines are inserted.

I just went ahead, turned the snippet into a function, and called it from there, which solved the problem entirely.

Comments
  • 3
    I don't get your explanation. Too bad, I'm really curious...
  • 3
    @ScriptCoded basically pasting in a single print line such as print(var) is fine, and will output a padded or justified string, without quotes. If you are consolepasting multiple lines at once however, print will insert quotes. And if that print statement is something like print(string1+string2), and one of those strings is empty or just spaces (for padding), then a linebreak will be inserted every time.
  • 1
    Sounds like np++ is doing that. Whatever that is.
  • 1
    I also think np++ is doing things. Migrate to geany (or vscode)
  • 0
    Please post a simple script doing this. I am sorta getting what you are talking about, but not quite.
  • 2
    Not sure...

    https://docs.python.org/3/library/...

    print in Python has sep and end to control the separator (multiple objects / arge) and end character.

    end character is by default system line separator, yes.

    Your problem sounds though to me more like console buffering interfering with output...

    https://docs.python.org/3/using/...

    See bug https://github.com/python/cpython/... for a better explanation and for the 3.7 change and better explanation of possible performance hits.
Add Comment