Write a Python program to split a given multiline string into a list of lines


The program takes a string that contains newline characters (\n) as input and uses the split() function to split the string into a list of substrings at each newline character. The resulting list of substrings is then printed to the console. The program first initializes a string variable str with four lines of text, each separated by a newline character.

The split() function is then called on the str variable with the argument '\n'. This argument specifies that the string should be split at each occurrence of the newline character (\n). The resulting list of substrings is then printed to the console using the print() function.

When the split() function is called with the '\n' argument, the resulting list contains each line of the original string as a separate element, with the newline characters removed. For example, the first element of the resulting list will be the string 'Tutor', the second element will be 'Joes', and so on.

In summary, this program takes a string that contains newline characters as input, splits it into a list of substrings at each newline character using the split() function with '\n' as the argument, and then prints the resulting list of substrings to the console.

Source Code

str = "Tutor\nJoes\nComputer\nEducation"
print(str.split('\n'))

Output

['Tutor', 'Joes', 'Computer', 'Education']


Example Programs