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']
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Python,
- Python List of Lists with Examples
- Fix: ERROR: Could not find a version that satisfies the requirement tkinter (from versions: none) ERROR: No matching distribution found for tkinter
- How to Convert Python Notebook .ipynb (JSON) to Executable .py Module
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- Python: Pandas Merge Indicator (Left, Right and Both) Example
- Fix: EOFError Exception in Python
- How to URL Decode a Query String in Python
- How to take user input from the console in a Python program
- Compare two lists in Python and return matches
- How to change the Python Default version
- Fix: KeyError: exception in Python
- How to get the Execution Time of A Python Program
- Fix: ModuleNotFoundError: No module named boto3 [Python]
- How to get unique values from a list in Python
- How to pip install Python Modules in VSCode
- Python: Fix - TypeError: NoneType object is not iterable
- How to delete a file using Python code example
- How to create a dictionary comprehension in Python
- Python: Get just the filename without extension using Path
- How to comment out a block of code in Python
- How to add two float numbers in Python
- Python: How to POST Json Data with HTTP Request
- Get MD5 Hash as Checksum of a String in Python
- Python - Convert float to String
- How to Parse XML String in Python
More Posts:
- [IRCTC] Indian railways official eRail API 1.1 for developers to get train info - HowTos
- Python: Convert Binary String to Normal String - Python
- Duplicate id @+id/textView1, already defined earlier in this layout Android Error - Android
- Find Covid-19 Vaccine centers on macOS or iOS Maps App - News
- How to Add Maven Central Repository in pom.xml - HowTos
- Not receiving email notification alert in SharePoint Online workflow - Power Automate, FLOW - SharePoint
- How to show SSL Certificate details using cURL Command - cURL
- PowerShell Concatenate String Examples - Powershell