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()
# Python code to demonstrate # to split strings# on newline delimiter # Initialising stringini_str = 'Geeks\nFor\nGeeks\n' # Printing Initial stringprint ("Initial String", ini_str) # Splitting on newline delimiterres_list = ini_str.splitlines() # Printing resultprint("Resultant prefix", str(res_list)) |
Output:
Initial String Geeks For Geeks Resultant prefix ['Geeks', 'For', 'Geeks']
Method #2: Using split() method
# Python code to demonstrate # to split strings# on newline delimiter # Initialising stringini_str = 'Geeks\nFor\nGeeks\n' # Printing Initial stringprint ("Initial String", ini_str) # Splitting on newline delimiterres_list = (ini_str.rstrip().split('\n')) # Printing resultprint("Resultant prefix", str(res_list)) |
Output:
Initial String Geeks For Geeks Resultant prefix ['Geeks', 'For', 'Geeks']
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course


