Is there a way to stop input() from escaping \n?

守給你的承諾、 提交于 2020-07-16 10:41:11

问题


So I'm trying to allow my user to input \n in a string, to designate a new line. However for some reason that is translated into '\\n'. So I can do:

inp = input().replace("\\n", "\n")

but that seems a little cumbersome; is there a better way?


回答1:


What you're looking for here is actually almost the opposite of what you're asking for.

input does not escape the string in any way; it returns exactly the string the user typed.

And, in fact, your attempted fix for that is, while probably not the best solution, fine.


I think your confusion is that you're mixing up the concept of string literals in source code with actual string values:

I was under the impression that \n in a string leads to a new line being triggered when that string is printed & that the \n in e.g. "exam\nple" is just a string and nothing else.

No, \n in a string does not lead to a new line being triggered when the string is printed; only a newline character does that.

But \n in a string literal in your source code doesn't give you \n in the string, it gives you a newline character in the string.

When you write this:

>>> spam = "exam\nple"

… Python treats the \n as an escape sequence, and gives you a string value containing a newline character. So:

>>> print(spam)
exam
ple

The string doesn't have a backslash and an n in it, it has a newline in it. If you had a string that did have a backslash and an n in it, it would print that. For example:

>>> spam = r"exam\nple"
>>> print(spam)
exam\nple
>>> spam = "exam" + chr(92) + "nple"
>>> print(spam)
exam\nple

And that's exactly what's happening here: input is, again, giving you exactly the characters the user typed, including a backslash and an n.

You can confuse yourself even more by looking at the repr of a string, because the repr doesn't just show you the string, it shows you a string literal that you could feed into Python to get back the string:

>>> spam = "exam\nple"
>>> print(spam)
exam
ple
>>> spam
'exam\nple'

So, that explains why this works:

inp = input().replace("\\n", "\n")

It's not that the user's string has \\n in it, it's that the string literal "\\n" in your source is a backslash and an n, which matches what's in the user's input, while the string literal "\n" in your source is a newline, which is what you want to replace the backslash and n with.


What you're looking for is to allow the user to manually escape their input, in the same way you would do in a literal string in your source code, and then have your code apply those escape sequences, in the same way Python does to turn string literals in your source code into string values.

You can do that by manually processing escape sequences, like your code:

inp = input().replace("\\n", "\n")

But that's ugly if you want to handle other escapes. The simplest way to do that is to apply the unicode-escape codec, like this:

inp = codecs.decode(input(), "unicode-escape")

Now, if the user types abc\ndef, that will be unescaped into abc, a newline, and def.

Either way, if you want to do this many times, you can write a wrapper function:

def unescaped_input(prompt=""):
    return codecs.decode(input(prompt), "unicode-escape")

… and now:

inp = unescaped_input()


来源:https://stackoverflow.com/questions/52248391/is-there-a-way-to-stop-input-from-escaping-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!