In Python, you can split a string by new line using the split
method with the newline character '\n'
.
In this example,
str
with a value containing new lines.split
method with the newline character '\n'
to split the string into a list of substrings.str = 'Hello\nWorld\n'
substrings = str.split('\n')
for sub in substrings:
print(sub)
Hello World
In this tutorial, we learned How to Split a String by New Line in Python language with well detailed examples.