Python: Split a multiline string into a list of lines
Split multiline string to lines.
Write a Python program to split a multi-line string into a list of lines.
- Use str.split() and '\n' to match line breaks and create a list.
- str.splitlines() provides similar functionality to this snippet.
Sample Solution:
Python Code:
Sample Output:
Original string: This is a multiline string. Split the said multiline string into a list of lines: ['This', 'is a', 'multiline', 'string.', '']
Flowchart:
For more Practice: Solve these Related Problems:
- Write a Python program to split a multi-line string into a list of its individual lines using splitlines().
- Write a Python program to use the split('\n') method to separate a multi-line text into a list of lines.
- Write a Python program to remove trailing newline characters and split a text into lines stored in a list.
- Write a Python program to implement a function that takes a multi-line string and returns a list containing each line.
Go to:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to decapitalize the first letter of a given string.
Next: Write a Python program to check whether any word in a given sting contains duplicate characrters or not. Return True or False.
Python Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.