>>> convo = "Bob: Hello \n Sandy: How are you? \n Bob: Confused by a python problem"
>>> for line in convo.splitlines():
... print(line)
...
Bob: Hello
Sandy: How are you?
Bob: Confused by a python problem
>>>
Return a list of the lines in the string, breaking at line boundaries. This method
uses the universal newlines approach to splitting lines. Line breaks are not included in the resulting list unless keepends is given and true.
>>> convo = "Bob: Hello \n Sandy: How are you? \n Bob: Confused by a python problem"
>>> for line in convo.splitlines():
... print(line)
...
Bob: Hello
Sandy: How are you?
Bob: Confused by a python problem
您可以使用
str.splitlines
:从docs:
使用^{} 按新行拆分字符串:
其中,
splitlines()
使用通用换行符来拆分字符串,这意味着它将支持不同平台的换行符约定。演示:
相关问题 更多 >
编程相关推荐