Skip to content
Related Articles

Related Articles

Python | Ways to split strings using newline delimiter

View Discussion
Improve Article
Save Article
Like Article
  • 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']

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!