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 :
- Use the splitlines() method to split the ini_str string into a list of substrings. The splitlines() method is a built-in Python method that splits a string into substrings based on newline characters (\n) and returns the substrings as a list.
- Assigns the resulting list of substrings to a variable called res_list.
- Finally, the prints the resulting list using the print() function and the message “Resultant prefix“. The str() function is used to convert the list to a string before printing it.
Below is the implementation of the above approach:
Python3
ini_str = 'Geeks\nFor\nGeeks\n'
print ("Initial String", ini_str)
res_list = ini_str.splitlines()
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
Python3
ini_str = 'Geeks\nFor\nGeeks\n'
print ("Initial String", ini_str)
res_list = (ini_str.rstrip().split( '\n' ))
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.
Python3
import re
ini_str = 'Geeks\nFor\nGeeks\n'
print ( "Initial String" , ini_str)
res_list = re.split(r '\n' , ini_str)
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:
- Import the re module.
- Initialize a string ini_str with the value “Geeks\nFor\nGeeks\n”.
- Print the initial string using the print() function with the message “Initial String:” and the value of ini_str.
- Use the re.findall() method with the regular expression pattern [^\n]+ and the ini_str string as input to split the string on the newline delimiter and obtain a list of non-overlapping substrings.
- Assign the resulting list to a variable res_list.
- Print the resultant list using the print() function with the message “Resultant list:” and the value of res_list.
Below is the implementation of the above approach:
Python3
import re
ini_str = 'Geeks\nFor\nGeeks\n'
print ( "Initial String:" , ini_str)
res_list = re.findall(r '[^\n]+' , ini_str)
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:
- Import the StringIO module and csv.reader() function.
- Create an instance of the StringIO object with the initial string as its argument.
- Pass the instance of StringIO object to csv.reader() function, along with the delimiter as the newline character ‘\n’.
- Convert the resulting csv.reader object to a list using the list() function.
- Print the list.
Below is the implementation of the above approach:
Python3
from io import StringIO
import csv
ini_str = 'Geeks\nFor\nGeeks\n'
print ( "Initial String" , ini_str)
res_list = list (csv.reader(StringIO(ini_str), delimiter = '\n' ))
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.
Last Updated :
05 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...