Related Articles

Related Articles

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

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

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :