小编Xtr*_*eme的帖子

字符串中的"\n"不起作用

我有一小段代码用于我的操作系统:

print("Type your document below.")
print("Press enter to save.")
print("Type \\n for a new line.")
file=input()
print("Enter a file name...")
filename=input()
outFile = open(filename, "w+")
outFile.write(file)
outFile.close()
Run Code Online (Sandbox Code Playgroud)

但是当我运行这段代码(在def中)时,请说我输入如下内容:

foo \n bar
Run Code Online (Sandbox Code Playgroud)

因为当收到用户的输入时输入不起作用,所以你必须使用\n.

该文件结果如下:

foo \n bar
Run Code Online (Sandbox Code Playgroud)

代替:

foo
bar
Run Code Online (Sandbox Code Playgroud)

python string newline python-3.x

8
推荐指数
3
解决办法
1万
查看次数

标签 统计

newline ×1

python ×1

python-3.x ×1

string ×1