Skip to content
Related Articles

Related Articles

Improve Article
Python | Ways to split strings using newline delimiter
  • Difficulty Level : Basic
  • Last Updated : 30 Jun, 2019

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 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']

 
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']

 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




My Personal Notes arrow_drop_up