我使用以下方式获得用户输入:
paragraphInput = input ("paste your paragraph ")
print(paragraphInput)
我明白了:
Line 1 of the paragraph
Line 2 of the paragraph
Line 3 of the paragraph
Line 4 of the paragraph
我想展示这样的内容:
This is Line 1 of the paragraph !
This is Line 2 of the paragraph !
This is Line 3 of the paragraph !
This is Line 4 of the paragraph !
所以我想使用for
循环,但我不知道如何检索该行" n"该段落然后在其前面加This is
并在!
之后添加。有办法做到这一点吗?因为段落的行数会根据用户而改变......
感谢您花时间阅读并寻求帮助!
答案 0 :(得分:3)
Python具有分割字符串行的功能。
https://docs.python.org/2/library/stdtypes.html#str.splitlines
例如,
'ab c\n\nde fg\rkl\r\n'.splitlines()
回报
['ab c', '', 'de fg', 'kl'],
然后你可以遍历这个列表,现在有正确的换行符。
for line in paragraphInput.splitlines():
print "This is " + line + " !"
答案 1 :(得分:2)
你可以做到
var parentHeader= $("head").html();
var parentDivContent = $("#printer_data_1").html();
var newW= window.open("", "newW", "scrollbars=1,width=200,height=200");
var html = "<html>"
+ parentHeader // `parentHeader` `head` `html`
+ "<body><div>"
+ parentDivContent
+ "</div></body></html>"
newW.document.write(html);
newW.document.close();
newW.print();
newW.close();