Use the split () method to split strings in Python
This article describes how to use the split () method to split strings in Python. It is a basic knowledge in Python. For more information, see
The list of all words in the string returned by the split () method. If str is used as the separator (if not all spaces are separated), you can specify the number of words to be separated by num.
Syntax
The syntax of the split () method is as follows:
?
|
1 |
Str. split (str = "", num = string. count (str )).
|
Parameters
Str -- this is any separator, which is a space by default.
Num -- this is the number of rows to be split.
Return Value
This method returns the row list.
Example
The following example demonstrates the use of the split () method.
?
|
1
2
3
4
5 |
#! /Usr/bin/python
Str = "Line1-abcdef \ nLine2-abc \ nLine4-abcd ";
Print str. split ();
Print str. split ('', 1 );
|
When we run the above program, it will produce the following results:
?
|
1
2 |
['Line1-abcdef', 'line2-abc', 'line4-abcd']
['Line1-abcdef ',' \ nLine2-abc \ nLine4-abcd ']
|