How to copy paste multi-line text input without arbitrarily adding `\n` in jupyter-notebook?

Viewed 40

I am trying to take an interactive multi-line user input from a jupyter-notebook cell as follows:

contents = []
while True:
    line = input()
    if line:
        contents.append(line)
    else:
        break
        
input_text = "\n".join(contents)

Example input text:

This is a multi-line input.
It is good to put several lines after each other. 
The reason of this is a good indentation.
One can use both tabs and spaces.

But when I copy paste a multi-line input from somewhere instead of passing it line by line manually, it concatenates into a space separated string like below:

enter image description here

How do I establish copy pasting multi-line text input without arbitrarily adding \n after each line?

1 Answers

A dirty work-around is possible, if you are able to pre-process the user input by adding '\r\n' or some other predefined delimiter that could be split afterwards

This is a multi-line input.\r\n
It is good to put several lines after each other.\r\n
The reason of this is a good indentation.\r\n
One can use both tabs and spaces.

you could use decoding

print(input().encode().decode('unicode_escape'))

Resulting output:

This is a multi-line input.
It is good to put several lines after each other.
The reason of this is a good indentation.
One can use both tabs and spaces.
Related