1

I'd like to read user input as a string, that behaves as if I had assigned it like this:

my_string = "line1\r\nline2"

If I print this, it will create two lines. I can't manage to read a user input, that behaves the same way, neither with input() nor with sys.stdin.read().

>>> buffer = input()
line1\r\nline2
>>> print(buffer)
line1\r\nline2
>>> print("line1\r\nline2")
line1
line2
>>>

EDIT: I don't want to read multiple lines, I want to read one line, that contains a new line escape sequence and prints as two lines.

Konqi
  • 93
  • 1
  • 8
  • Are you literally typing the backslashes? Or is the question how to put a newline within the input by typing it? – OneCricketeer Aug 29 '19 at 03:56
  • Possible duplicate of [How do I read multiple lines of raw input in Python?](https://stackoverflow.com/questions/11664443/how-do-i-read-multiple-lines-of-raw-input-in-python) – OneCricketeer Aug 29 '19 at 03:57
  • I'm typing them right now, but It's not working the way I want it to. I want to input a string, that prints in two lines later. And I'd prefer to read that input in one line. – Konqi Aug 29 '19 at 03:59
  • You would have to replace `\\r\\n` with `\r\n` if you do want to only type on one line – OneCricketeer Aug 29 '19 at 04:01

1 Answers1

1

You can encode the string into bytes and then decode the bytes with the unicode_escape encoding:

>>> buffer = input()
line1\r\nline2
>>> print(buffer.encode().decode('unicode_escape'))
line1
line2
blhsing
  • 91,368
  • 6
  • 71
  • 106