Open In App

Ways to split strings using newline delimiter - Python

Last Updated : 12 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The goal is to break down the string wherever a newline character (\n, \r, or \r\n) appears, making it easier to process each line individually. For example, in the string "line1\nline2\rline3\r\nline4", different newline characters are used and we aim to efficiently split this string into separate lines. Let's explore different methods to split strings based on newline delimiters.

Using str.splitlines()

splitlines() method is the most reliable and cross-platform way to split strings by any newline character, including \n, \r, or \r\n. It intelligently detects all common line endings.

Python
s = "python\njava\rphp"

res = s.splitlines()
print(res)

Output
['python', 'java', 'php']

Explanation: This code splits the string s into lines using splitlines(), which detects all newline characters (\n, \r, \r\n). It separates "python", "java" and "php" into a list ["python", "java", "php"] based on these delimiters.

Using str.split('\n')

This is the most basic and direct string splitting approach. When you use split('\n'), Python breaks the string at every occurrence of the \n character which is the standard newline character in Unix/Linux and macOS.

Python
s = "python\njava\nphp"

res = s.split('\n')
print(res)

Output
['python', 'java', 'php']

Explanation: split('\n') split the string s wherever a newline character \n is found. It breaks the string into ["python", "java", "php"], separating the text at each \n.

Using re.split()

This method uses Python’s re (regular expression) module to define custom splitting patterns. It is best suited for splitting based on multiple types of line endings simultaneously, such as \n, \r or \r\n.

Python
import re

s = "line1\nline2\rline3\r\nline4"
res = re.split(r'\r\n|\n|\r', s)

print(res)

Output
['line1', 'line2', 'line3', 'line4']

Explanation: re.split() with the pattern r'\r\n|\n|\r' to split the string s at all common newline types.

Using str.split()

This method, when called without any arguments, splits a string on any whitespace character: spaces, tabs, newlines (\n), carriage returns (\r), etc. It's not designed for line-based splitting, but can be useful in some specific scenarios.

Python
s = "line1\nline2\n\nline3"

res = s.split()
print(res)

Output
['line1', 'line2', 'line3']

Explanation: This code splits s by all whitespace and removes empty lines, giving ["line1", "line2", "line3"].


Practice Tags :

Similar Reads