打印"\ n"或换行符作为终端输出的一部分 [英] Print "\n" or newline characters as part of the output on terminal

查看:308
本文介绍了打印"\ n"或换行符作为终端输出的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在终端上运行Python

I'm running Python on terminal

给出一个字符串string = "abcd\n"

我想以某种方式print,以便abcd\n中的换行符'\n'可见,而不是转到下一行

I'd like to print it somehow so that the newline characters '\n' in abcd\n would be visible rather than go to the next line

我可以这样做而不必修改字符串并添加双斜杠(\\n)

Can I do this without having to modify the string and adding a double slash (\\n)

推荐答案

使用 repr

>>> string = "abcd\n"
>>> print(repr(string))
'abcd\n'

这篇关于打印"\ n"或换行符作为终端输出的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆