There are multiple ways in which we can split a string in Python based on the delimiter, let's take a look at some of them with examples.
Example 1: Using the split() function
>>> string_data = "1,2,3,4,5,6,7,8,9,10"
>>> splitted_string = string_data.split(",")
>>> print(splitted_string)
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
>>>
>>> print(type(splitted_string))
<class 'list'>
>>>
The most common delimiters that one comes across when parsing data line by line as strings in files are,
| Delimiter | Symbol | Description |
|---|---|---|
| Comma | , | Used to separate values in CSV files |
| Tab | \t | Used as a delimiter in TSV files |
| Pipe | | | Commonly used in data interchange formats |
| Semicolon | ; | Often used in European CSV files |
| Colon | : | Used in some data formats and configurations |
| Space | Separates values in space-delimited files | |
| Hyphen | - | Used in certain data representations |
| Underscore | _ | Occasionally used as a separator |
| Dot | . | Used in some file extensions |
You can simply use the symbol like these as an argument to the split function.
Let's take another example where data is separated by pipe as a delimiter.
Example:>>> string_data = "1|2|3|4|5|6|7|8|9|10"
>>> splitted_string = string_data.split("|")
>>> print(splitted_string)
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!