
- 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 string on whitespace in Python?
You can usesplit() from the string class. The default delimiter for this method is whitespace, i.e., when called on a string, it'll split that string at whitespace characters. For example:
>>>"Please split this string".split() ['Please','split', 'this', 'string']
Regex can also be used to solve this problem. You can call the re.split() method using the regex '\s+' as delimiter. Note that this method is slower than the above method.
>>>import re >>>re.split('\s+', 'Please split this string') ['Please','split', 'this', 'string']
- Related Questions & Answers
- How to split a string in Python
- How to remove all leading whitespace in string in Python?
- How to remove all trailing whitespace of string in Python?
- How do we split a string with any whitespace chars as delimiters using java?
- C# Program to split a string on spaces
- How to check if a string contains only whitespace letters in Python?
- How to split strings on multiple delimiters with Python?
- Remove whitespace in Java string.
- How to remove all whitespace from String in Java?
- How to remove all trailing and leading whitespace in a string in Python?
- Converting whitespace string to url in JavaScript
- How to split string by a delimiter str in Python?
- How to split a string in Java?
- How to split a string in Golang?
- Split String of Size N in Python
Advertisements