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()
# 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
filter_none
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)) |
chevron_right
filter_none
Output:
Initial String Geeks For Geeks Resultant prefix ['Geeks', 'For', 'Geeks']
Recommended Posts:
- Python | Ways to split strings on Uppercase characters
- Python | Split on last occurrence of delimiter
- Python | Pandas Split strings into two List/Columns using str.split()
- Python | Ways to split a string in different ways
- Python | Split Sublist Strings
- Python | Reversed Split Strings
- Python | Split strings in list with same prefix in all elements
- Python | Split CamelCase string to individual strings
- Python | Split list of strings into sublists based on length
- How to print without newline in Python?
- Python | Pandas Reverse split strings into two List/Columns using str.rsplit()
- Python | Ways to merge strings into list
- Python | Ways to determine common prefix in set of strings
- Python | Ways to sort list of strings in case-insensitive manner
- Python | Ways to convert array of strings to array of floats
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.



