In Python, the split()
method is a built-in function used to divide a string into a list of substrings based on a specified delimiter. By default, the delimiter is any whitespace, and it splits the string wherever it encounters spaces, tabs, or newlines.
Syntax:
string.split(separator, maxsplit)
- separator (optional): The delimiter you want to use to split the string. By default, it is any whitespace (spaces, tabs, newlines).
- maxsplit (optional): The number of splits to perform. The default value is -1, which means “all occurrences”. If you specify a number, it will split only that many times.
Examples:
1. Basic Example with Default Separator (Whitespace)
By default, split()
divides the string wherever there are spaces.
text = "Hello world! Welcome to Python."
result = text.split()
print(result)
Output:
['Hello', 'world!', 'Welcome', 'to', 'Python.']
Here, the string is split at each whitespace.
2. Splitting with a Specific Separator
You can provide a specific separator to split the string. For example, splitting a sentence by commas:
text = "apple,banana,cherry,dates"
result = text.split(',')
print(result)
Output:
['apple', 'banana', 'cherry', 'dates']
In this case, the string is split wherever there is a comma (,
).
3. Using maxsplit
to Limit the Number of Splits
You can also limit the number of splits performed by specifying the maxsplit
argument. For example, splitting the string into only two parts:
text = "apple,banana,cherry,dates"
result = text.split(',', 2)
print(result)
Output:
['apple', 'banana', 'cherry,dates']
Here, the string is split into a maximum of 2 parts: ['apple', 'banana', 'cherry,dates']
.
4. Splitting with Multiple Whitespace Characters
The split()
method can also handle multiple spaces, treating them as a single separator:
text = "This is a sentence."
result = text.split()
print(result)
Output:
['This', 'is', 'a', 'sentence.']
Notice that multiple spaces between words are automatically ignored, and the string is split correctly.
5. Splitting an Empty String
If the string is empty, split()
returns an empty list:
text = ""
result = text.split()
print(result)
Output:
[]
6. Splitting by Newlines
You can also use split()
to divide a string by newline characters (\n
):
text = "Line 1\nLine 2\nLine 3"
result = text.split('\n')
print(result)
Output:
['Line 1', 'Line 2', 'Line 3']
7. Splitting Strings with Different Separators
If you have a string with different kinds of separators (e.g., commas and spaces), you can use regular expressions (via the re.split()
function) to split the string. This is not directly done with the split()
method, but it’s a useful approach for more complex scenarios.
import re
text = "apple, banana; orange|grape"
result = re.split('[,;|]', text)
print(result)
Output:
['apple', ' banana', ' orange', 'grape']
In this case, we used regular expressions to split the string by commas, semicolons, or vertical bars (|
).
Key Takeaways:
split()
is used to divide a string into substrings based on a delimiter.- If no delimiter is provided, it splits by any whitespace.
- You can limit the number of splits using the
maxsplit
parameter. - The method handles multiple consecutive spaces as a single separator.
The split()
method is one of the most commonly used string manipulation methods in Python and is very versatile for splitting strings in various ways.