Description
Python string method split() returns a list of all the words in the string, using str as the separator (splits on all whitespace if left unspecified), optionally limiting the number of splits to num.
Syntax
Following is the syntax for split() method −
str.split(str="", num=string.count(str)).Parameters
str − This is any delimeter, by default it is space.
num − this is number of lines minus one
Return Value
This method returns a list of lines.
Example
The following example shows the usage of split() method.
#!/usr/bin/python str = "Line1-abcdef \nLine2-abc \nLine4-abcd"; print str.split( ) print str.split(' ', 1 )When we run above program, it produces following result −
['Line1-abcdef', 'Line2-abc', 'Line4-abcd'] ['Line1-abcdef', '\nLine2-abc \nLine4-abcd']python_strings.htm
I'm looking for the Python equivalent of
String str = "many fancy word \nhello \thi"; String whiteSpaceRegex = "\\s"; String[] words = str.split(whiteSpaceRegex); ["many", "fancy", "word", "hello", "hi"]
Martin Thoma
113k148 gold badges570 silver badges874 bronze badges
asked Nov 13, 2011 at 18:46
0
The str.split() method without an argument splits on whitespace:
>>> "many fancy word \nhello \thi".split() ['many', 'fancy', 'word', 'hello', 'hi']
answered Nov 13, 2011 at 18:46
Sven MarnachSven Marnach
544k114 gold badges914 silver badges816 bronze badges
9
import re s = "many fancy word \nhello \thi" re.split('\s+', s)
answered Nov 13, 2011 at 18:49
Óscar LópezÓscar López
228k35 gold badges304 silver badges377 bronze badges
3
Using split() will be the most Pythonic way of splitting on a string.
It's also useful to remember that if you use split() on a string that does not have a whitespace then that string will be returned to you in a list.
Example:
>>> "ark".split() ['ark']answered Feb 21, 2017 at 14:18
digitalnomddigitalnomd
1,33212 silver badges19 bronze badges
Another method through re module. It does the reverse operation of matching all the words instead of spitting the whole sentence by space.
>>> import re >>> s = "many fancy word \nhello \thi" >>> re.findall(r'\S+', s) ['many', 'fancy', 'word', 'hello', 'hi']Above regex would match one or more non-space characters.
answered Jun 17, 2015 at 18:33
Avinash RajAvinash Raj
169k25 gold badges214 silver badges262 bronze badges
Contents
- Introduction
- Example 1: Split String by Space
- Example 2: Split String by One or More Adjacent Spaces
- Example 3: Split String by Any White Space Character
- Summary
You can split a string with space as delimiter in Python using String.split() method.
In this tutorial, we will learn how to split a string by a space character, and whitespace characters in general, in Python using String.split() and re.split() methods.
Refer Python Split String to know the syntax and basic usage of String.split() method.
Example 1: Split String by Space
In this example, we will take a string which contains words/items/chunks separated by space character. We shall then split the string by space using String.split() method. split() method returns list of chunks.
Python Program
str = '63 41 92 81 69 70' #split string by single space chunks = str.split(' ') print(chunks)Run
Output
['63', '41', '92', '81', '69', '70']Example 2: Split String by One or More Adjacent Spaces
In this example, we will take a string with chunks separated by one or more single space characters. Then we shall split the string using re.split() function. re.split() returns chunks in a list.
We shall use re python package in the following program. re.split(regular_expression, string) returns list of chunks split from string based on the regular_expression.
Python Program
import re str = '63 41 92 81 69 70' #split string by single space chunks = re.split(' +', str) print(chunks)Run
Regular Expression + represents one or more immediately occuring spaces. So, one or more single space characters is considered as a delimiter.
Output
['63', '41', '92', '81', '69', '70']One ore more adjacent spaces are considered as a single delimiter because of the regular expression.
Example 3: Split String by Any White Space Character
In this example, we shall split the string into chunks with any white space character as delimiter.
Following are the list of white space characters from ASCII Table.
ASCII Hex Code | Description |
09 | horizontal tab |
0A | New line feed |
0B | Vertical Tab |
0D | Carriage Return/ Form Feed |
20 | Space |
By default, String.split(), with no argument passed, splits the string into chunks with all the white space characters as delimiters.
Python Program
import re str = '63 41\t92\n81\r69 70' #split string by single space chunks = str.split() print(chunks)Run
Output
['63', '41', '92', '81', '69', '70']Summary
In this tutorial of Python Examples, we learned how to split a string by space using String.split() and re.split() methods. Also, we learned how to split a string by considering all whitespace characters as delimiter.
Related Tutorials
- Python Split String into Specific Length Chunks
- Python Split String by Comma
- How to Split String by Underscore in Python?
- Python Split String into List of Characters
- Python Split String by New Line