Change language

Python | Ways to split lines with newline separator

| |

Method # 1: Using splitlines()

# Python code for demo
# split lines
# on a new line

 
# Initializing string

ini_str = ’Geeks For Geeks’

 
# Print start line

print ( "Initial String" , ini_str)

 
# Splitting on newline separator

res_list = ini_str.splitlines ()

 
# Print result

print ( "Resultant prefix" , str (res_list))

Exit:

 Initial String Geeks For Geeks Resultant prefix [’Geeks’,’ For’, ’Geeks’] 

Method # 2: Using the split () method

# Python code for demonstration
# split lines
# on a new line

 
# Initializing string

ini_str = ’Geeks For Geeks’

 
# Print start line

print ( "Initial String" , ini_str)

 
# Separated by newline separator

res_list = (ini_str.rstrip (). split ( ’ ’ ))

  
# Print result

print ( " Resultant prefix " , str (res_list))

 

Exit:

 Initial String Geeks For Geeks Resultant prefix [’Geeks’ , ’For’,’ Geeks’]