Konqi
Konqi

Reputation: 103

How can I read userinput with the newline in python, that prints as a new line?

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.

Upvotes: 1

Views: 123

Answers (1)

blhsing
blhsing

Reputation: 106553

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

Upvotes: 1

Related Questions