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()

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:



Initial String Geeks
For
Geeks

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

 
Method #2: Using split() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

Initial String Geeks
For
Geeks

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


My Personal Notes arrow_drop_up

Programming freaktech Enthusiast and have interest in learning new upcoming technologies

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.