# How to split a string in Python

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

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.

Live Demo

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']
Updated on 23-Aug-2019 12:38:50