Open In App

Python | Ways to split strings using newline delimiter

Given a string, write a Python program to split strings on the basis of newline delimiter. Given below are a few methods to solve the given task. 

Method #1: Using splitlines() 



Step-by-step approach :

Below is the implementation of the above approach:






# Python code to demonstrate
# to split strings
# on newline delimiter
 
# Initialising string
ini_str = 'Geeks\nFor\nGeeks\n'
 
# Printing Initial string
print ("Initial String", ini_str)
 
# Splitting on newline delimiter
res_list = ini_str.splitlines()
 
# Printing result
print("Resultant prefix", str(res_list))

Output:
Initial String Geeks
For
Geeks

Resultant prefix ['Geeks', 'For', 'Geeks']

Time complexity: O(n) – where n is the length of the input string.
Auxiliary Space: O(n) – the split string is stored in a list, which requires additional space in memory.

Method #2: Using split() method 




# Python code to demonstrate
# to split strings
# on newline delimiter
 
# Initialising string
ini_str = 'Geeks\nFor\nGeeks\n'
 
# Printing Initial string
print ("Initial String", ini_str)
 
# Splitting on newline delimiter
res_list = (ini_str.rstrip().split('\n'))
 
# Printing result
print("Resultant prefix", str(res_list))

Output:
Initial String Geeks
For
Geeks

Resultant prefix ['Geeks', 'For', 'Geeks']

Time Complexity: O(n), where n is the length of the input string.
Auxiliary Space: O(1)

Method 3: Use the re module 

This will produce the same output as the splitlines() method. The re.split() function takes a regular expression as the delimiter, which in this case is simply \n.




import re
 
# Initialising string
ini_str = 'Geeks\nFor\nGeeks\n'
 
# Printing Initial string
print("Initial String", ini_str)
 
# Splitting on newline delimiter using re.split()
res_list = re.split(r'\n', ini_str)
 
# Printing result
print("Resultant prefix", str(res_list))

Output
Initial String Geeks
For
Geeks

Resultant prefix ['Geeks', 'For', 'Geeks', '']

Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), as the result list res_list will have the same length as the input string ini_str.

Method 4: Using the re.findall() method with a regular expression

The program uses the re module to split a string on the newline delimiter using the re.findall() method with the regular expression pattern [^\n]+. The initial string and the resultant list are printed using the print() function

Step-by-step approach:

Below is the implementation of the above approach:




# Importing the re module
import re
 
# Initialising string
ini_str = 'Geeks\nFor\nGeeks\n'
 
# Printing Initial string
print("Initial String:", ini_str)
 
# Using re.findall() method with regular expression pattern
res_list = re.findall(r'[^\n]+', ini_str)
 
# Printing result
print("Resultant list:", res_list)

Output
Initial String: Geeks
For
Geeks

Resultant list: ['Geeks', 'For', 'Geeks']

Time complexity: O(n) where n is the length of the input string
Auxiliary space: O(m) where m is the number of substrings obtained after splitting the input string on the newline delimiter.

Method #5: Using the StringIO module and the csv.reader() function

Step-by-step approach:

Below is the implementation of the above approach:




# Python code to demonstrate
# to split strings
# on newline delimiter
 
# import required modules
from io import StringIO
import csv
 
# Initialising string
ini_str = 'Geeks\nFor\nGeeks\n'
 
# Printing Initial string
print("Initial String", ini_str)
 
# Splitting using StringIO and csv.reader
res_list = list(csv.reader(StringIO(ini_str), delimiter='\n'))
 
# Printing result
print("Resultant prefix", str(res_list))

Output
Initial String Geeks
For
Geeks

Resultant prefix [['Geeks'], ['For'], ['Geeks']]

Time Complexity: O(n), where n is the length of the string ini_str.
Auxiliary Space: O(n), where n is the length of the string ini_str, as we are storing the string in the StringIO object.


Article Tags :