Split a String by a Delimiter in Python
Last Updated :
26 Feb, 2024
In Python Programming, the split() method is used to split a string into a list of substrings which is based on the specified delimiter. This method takes the delimiter as an argument and returns the list of substrings. Along with this method, we can use various approaches wot split a string by the specified delimiter in Python. In this article, we will explore different approaches to split a string by a delimiter in Python.
Split a String by a Delimiter in Python
Below are the possible approaches to split a string by a delimiter in Python:
- Using re.split() method
- Using str.strip() method
- Using csv.reader() method
Split a String by a Delimiter Using re.split() method
The below approach code uses re.split() method in Python, with the regular expression ',', used to split the string "Geeks,for,Geeks" at each comma, resulting in the list ['Geeks', 'for', 'Geeks'].
Python3
import re
data = "Geeks,for,Geeks"
result = re.split(',', data)
print(result)
Output['Geeks', 'for', 'Geeks']
Split a String by a Delimiter Using the str.strip() method
The below approach code uses split(',') to break the string "Geeks,for,Geeks" into substrings at each comma, and then applies strip() to remove leading and trailing whitespaces, producing the list ['Geeks', 'for', 'Geeks'].
Python3
data = "Geeks,for,Geeks"
result = [substring.strip() for substring in data.split(',')]
print(result)
Output['Geeks', 'for', 'Geeks']
Split a String by a Delimiter Using csv.reader() method
The below approach code uses csv.reader([data]) to interpret the string "Geeks,for,Geeks" as a CSV row, and list(...)[0] extracts the resulting list, printing ['Geeks', 'for', 'Geeks'].
Python3
import csv
data = "Geeks,for,Geeks"
result = list(csv.reader([data]))[0]
print(result)
Output['Geeks', 'for', 'Geeks']
Conclusion
In conclusion, the split() method in Python is a function for splitting strings into substrings based on a specified delimiter. The above approaches, including re.split(), str.strip(), and csv.reader(), offer similar functionality to split() function. These techniques help developers to efficiently manipulate and extract meaningful information from strings using various delimiter-based splitting methods in Python.
Similar Reads
Split a string on multiple delimiters in Python In this article, we will explore various methods to split a string on multiple delimiters in Python. The simplest approach is by using re.split().Using re.split()The re.split() function from the re module is the most straightforward way to split a string on multiple delimiters. It uses a regular exp
2 min read
Split and Parse a string in Python In this article, we'll look at different ways to split and parse strings in Python. Let's understand this with the help of a basic example:Pythons = "geeks,for,geeks" # Split the string by commas res = s.split(',') # Parse the list and print each element for item in res: print(item)Outputgeeks for g
2 min read
Difference Between strip and split in Python The major difference between strip and split method is that strip method removes specified characters from both ends of a string. By default it removes whitespace and returns a single modified string. Whereas, split method divides a string into parts based on a specified delimiter and by default it
1 min read
Splitting Concatenated Strings in Python List in Python are versatile data structures that can hold a collection of items, including strings. Text processing and natural language processing (NLP), are common tasks to split a concatenated string into its constituent words. This task can be particularly challenging when the string contains n
4 min read
How to split a string in C/C++, Python and Java? Splitting a string by some delimiter is a very common task. For example, we have a comma-separated list of items from a file and we want individual items in an array. Almost all programming languages, provide a function split a string by some delimiter. In C: // Splits str[] according to given delim
7 min read