In Python, the split()
method is a built-in function that allows us to split a string into a list of substrings based on a specified delimiter. The default delimiter is a space character, but it can be changed to any other character or string.
The split()
function takes two optional parameters: sep
and maxsplit
. The sep
parameter specifies the delimiter to be used for splitting the string, and the maxsplit
parameter specifies the maximum number of splits to be performed.
Syntax
The syntax for using the split()
function in Python is as follows:
string.split(sep=None, maxsplit=-1)
Here, string
is the string that needs to be split, sep
is the delimiter to be used for splitting the string, and maxsplit
is the maximum number of splits to be performed.
Examples
Example 1: Splitting a string using the default delimiter
string = "Hello World"
result = string.split()
print(result)
Output:
['Hello', 'World']
In this example, we have a string “Hello World”, and we are using the split()
function without any parameters, which means that the default delimiter (space) will be used to split the string. The output is a list of two substrings: Hello and World.
Example 2: Splitting a string using a custom delimiter
string = "apple,banana,orange"
result = string.split(',')
print(result)
Output:
['apple', 'banana', 'orange']
In this example, we have a string “apple,banana,orange”, and we are using the split()
function with a custom delimiter (comma). The output is a list of three substrings: apple, banana, and orange.
Example 3: Specifying the maximum number of splits
string = "apple,banana,orange,grape,kiwi"
result = string.split(',', 3)
print(result)
Output:
['apple', 'banana', 'orange', 'grape,kiwi']
In this example, we have a string “apple,banana,orange,grape,kiwi”, and we are using the split()
function with a custom delimiter (comma) and a maximum number of splits (3). The output is a list of four substrings: apple, banana, orange, and grape,kiwi.
Example 4: Splitting a string with multiple delimiters
Import the re
module:
import re
Here’s the example:
string = "apple,banana and orange"
result = re.split(',| and ', string)
print(result)
Output: ['apple', 'banana', 'orange']
In this example, we have a string “apple,banana and orange”, and we are using the split()
function with multiple delimiters (comma and ‘and’). We achieve this by using the re.split()
function from the re
module, which allows us to split a string using a regular expression. The output is a list of three substrings: apple, banana, and orange.
Example 5: Splitting a string into individual characters
string = "Hello World"
result = list(string)
print(result)
Output:
['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
In this example, we have a string “Hello World”, and we are using the split()
function to split the string into individual characters. We achieve this by converting the string to a list using the list()
function. The output is a list of individual characters.
Related Concepts
join()
The join()
function is used to join a list of substrings into a single string. It is the opposite of the split()
function. Here’s an example:
string = " ".join(['Hello', 'World'])
print(string)
# Hello World
strip()
The strip()
function is used to remove any leading or trailing whitespace from a string. It can be used in conjunction with the split()
function to remove whitespace around each substring. Here’s an example:
string = " Hello World "
result = [s.strip() for s in string.split()]
print(result)
# ['Hello', 'World']
Conclusion
The split()
function is a useful tool for working with strings in Python. It allows us to split a string into a list of substrings based on a specified delimiter. In this article, we covered the syntax, parameters, and usage of the split()
function, and we provided several examples to demonstrate its functionality. We also discussed some related concepts that can help you better understand the split()
function. With this knowledge, you can confidently use the split()
function in your Python projects.