How to escape line break already present in a string?

倾然丶 夕夏残阳落幕 提交于 2019-12-11 13:02:43

问题


I have a question, why does this:

testStr="\n"
testStr = "\\"+testStr
print testStr
>>> \

happen? Shouldn't it now print \n? I know about the repr() function, but I would rather solve this in another way It would be very kind if you could help me


回答1:


After you type testStr = "\n" the special characters are already being interpreted. So in the next line you cannot change their interpretation as it already has happened. This is being done during lexical analysis stage, so even way before the code is actually executed. When the string is being assigned to your variable the two characters "\" and "n" are already gone - there is only one character - the new line character.



来源:https://stackoverflow.com/questions/27363399/how-to-escape-line-break-already-present-in-a-string

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