
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to split a string in Python
Manytimes we need to split a given string into multiple parts based on some delimiter. Python provides a function named split() which can be used to achieve this. It also provides a way to control the delimiter and number of characters to be considered as delimiter.
Example
In the below example we a string containing many words and space in between. But there are two space characters between Banana and grape. Accordingly the split happens. When no parameter is supplied each space is taken as a delimiter.
str = "Apple Banana Grapes Apple"; print(str.split()) print(str.split(' ', 2))
Output
Running the above code gives us the following result −
['Apple', 'Banana', 'Grapes', 'Apple'] ['Apple', 'Banana', ' Grapes Apple']
- Related Articles
- How to split string by a delimiter string in Python?
- How to split string on whitespace in Python?
- How to split a string in Java?
- How to split a string in Golang?
- Python program to split and join a string?
- How to split a string with a string delimiter in C#?
- How to split a string in Lua programming?
- How do we use a delimiter to split string in Python regular expression?
- Python Program to Split a String by Custom Lengths
- Split a string in equal parts (grouper in Python)
- Program to find number of ways to split a string in Python
- Split String of Size N in Python
- How to split a string into elements of a string array in C#?
- How can I use Python regex to split a string by multiple delimiters?
- Python program to split a string and join with comma

Advertisements