
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- How to split string by a delimiter str in Python?
- How to split string on whitespace in Python?
- How to split a string in Java?
- How to split a string in Golang?
- How to split a semicolon-separated string to a dictionary in Python?
- Python program to split and join a string?
- How to split a string in Lua programming?
- How to split a string with a string delimiter in C#?
- Split a string in Java
- How to Split a String with Escaped Delimiters?
- Split a string in equal parts (grouper in Python)
- How do we use a delimiter to split string in Python regular expression?
- Python program to split a string and join with comma
- How to split a string into elements of a string array in C#?
- Split String of Size N in Python
Advertisements