【发布时间】:2015-08-24 17:22:29
【问题描述】:
如何在单词后添加空行或如何将短语拆分为特定单词的段落格式并将其值分配给另一个字符串
input: "hello"
output: "hello"
#empty line
input : "hello this is test" #after hello i want to split the data into new line
output : "hello
this is test" #New line and tab
我的代码:
string = "hello this is test"
string2 = string.replace("hello","hello \n\t")
但我的输出是
"'hello \n\t this is test'"
但是当我这样做时:
print(string2)
我的结果是我需要的格式
"hello
this is a test"
问题:
如何在不打印字符串的情况下做到这一点?
【问题讨论】:
-
你的字符串很好 - 当你在 Python 解释器中输入一个表达式时,它会显示值的 representation。
\n\t是 Python 的 representation 用于换行符和制表符。您的字符串是正确的,只需将其写入文件,通过网络发送或做任何您想做的事情。 -
你的代码就是你所需要的,字符串中的换行符由\n定义,制表由\t定义,只有当你打印它们时,它们才会被解释为输出格式。