DescriptionPython 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. Show
SyntaxFollowing is the syntax for split() method − str.split(str="", num=string.count(str)). Parameters
Return ValueThis method returns a list of lines. ExampleThe 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
Martin Thoma 113k148 gold badges570 silver badges874 bronze badges asked Nov 13, 2011 at 18:46
0 The
answered Nov 13, 2011 at 18:46
Sven MarnachSven Marnach 544k114 gold badges914 silver badges816 bronze badges 9
answered Nov 13, 2011 at 18:49
Óscar LópezÓscar López 228k35 gold badges304 silver badges377 bronze badges 3 Using It's also useful to remember that if you use Example:
answered Feb 21, 2017 at 14:18
digitalnomddigitalnomd 1,33212 silver badges19 bronze badges Another method through
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
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 SpaceIn 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
Run Output
Example 2: Split String by One or More Adjacent SpacesIn 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 Python Program
Run Regular Expression Output
One ore more adjacent spaces are considered as a single delimiter because of the regular expression. Example 3: Split String by Any White Space CharacterIn 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.
By default, String.split(), with no argument passed, splits the string into chunks with all the white space characters as delimiters. Python Program
Run Output
SummaryIn 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
How do you split a string by spaces?To split a string by multiple spaces, call the split() method, passing it a regular expression, e.g. str. trim(). split(/\s+/) . The regular expression will split the string on one or more spaces and return an array containing the substrings.
How do you read a string with spaces in Python?Python String isspace() method returns “True” if all characters in the string are whitespace characters, Otherwise, It returns “False”. This function is used to check if the argument contains all whitespace characters, such as: ' ' – Space.
How do you split one space or more in Python?You can also use a regular expression to split a string by one or more spaces. Use the re. split() method to split a string by one or more spaces, e.g. re. split(r'\s+', my_str) .
|