Python String split() Method

Python split() method splits the string into a comma separated list. It separates string based on the separator delimiter. Python is a popular programming language that offers a wide range of powerful tools and functionalities for developers. One of the most used methods in Python is the split() method, which allows you to split a string into substrings based on a specified separator. In this article, we'll take a closer look at the split() method in Python, how it works, and how you can use it to manipulate strings in your code.

This method takes two parameters, and both are optional. It is described below.

Signature

Parameters

sep: A string parameter acts as a seperator.

maxsplit: Number of times split perfome.

Return

It returns a comma separated list.

Let's see some examples of split() method to understand it's functionality.

Understanding the split() method

The split() method in Python is used to break down a string into smaller substrings. The substrings are determined by a separator that is specified within the method's parameters. By default, the separator is a space character, but it can be changed to any character or sequence of characters. The split() method returns a list of substrings that are separated by the specified separator.

The syntax for the split() method is as follows:

The first parameter is the separator, which is used to break the string into substrings. If no separator is specified, the default separator is a space character. The second parameter is optional and specifies the maximum number of splits that can be made. If this parameter is not provided, then there is no limit to the number of splits that can be made.

Let's look at some examples of using the split() method in Python:

Example:

Output:

['Hello', 'World']

In this example, we use the split() method to break down the string "Hello World" into two substrings: "Hello" and "World". Since we didn't specify a separator, the default separator (space character) was used.

Let's see some examples of split() method to understand it's functionality.

Python String split() Method Example

This is a simple example to understand the usage of split() method. No parameter is given, by default whitespaces work as separator. See the example below.

Output:

Java is a programming language
['Java', 'is', 'a', 'programming', 'language']

Python String split() Method Example 2

Let's pass a parameter separator to the method, now it will separate the string based on the separator. See the example below.

Output:

['', ' is a programming language']

Python String rsplit() Method Example 3

The string is splited each time when a is occurred. See the example below.

Output:

Java is a programming language
['J', 'v', ' is ', ' progr', 'mming l', 'ngu', 'ge']

Example :

Output:

['Pyth', 'n is awes', 'me']

In this example, we use the split() method to break down the string "Python is awesome" into three substrings: "Pyth", "n is awes", and "me". We specified the letter "o" as the separator, so the split() method splits the string at each occurrence of the letter "o".

Using the maxsplit parameter

As mentioned earlier, the split() method has an optional parameter called maxsplit, which specifies the maximum number of splits that can be made. Let's take a look at an example:

Output:

['John', 'Doe', 'Jane,Doe']

In this example, we used the maxsplit parameter to limit the number of splits to 2. As a result, the split() method only split the string at the first two commas it encountered.

Python String split() Method Example 4

Along with separator, we can also pass maxsplit value. The maxsplit is used to set the number of times to split.

Output:

['J', 'va is a programming language']
['J', 'v', ' is ', ' programming language']

Example :

Output:

['John', 'Doe', 'Jane', 'Doe']

In this example, we use the split() method to break down the string "John,Doe,Jane,Doe" into four substrings: "John", "Doe", "Jane", and "Doe". We specified the comma (",") as the separator, so the split() method splits the string at each comma.

Using the join() method with split()

The split() method is often used in conjunction with the join() method to manipulate strings in Python. The join() method is used to join a list of strings into a single string, using a specified separator. The separator is specified as a string within the join() method's parameters.

Output:

'John-Doe-Jane-Doe'

In this example, we first use the split() method to split the string "John,Doe,Jane,Doe" into a list of substrings. We then use the join() method to join the substrings back into a single string, using the hyphen ("-") as the separator.

Conclusion

The split() method in Python is a powerful tool for manipulating strings in your code. It allows you to break down a string into smaller substrings based on a specified separator. By default, the separator is a space character, but it can be changed to any character or sequence of characters. The split() method returns a list of substrings that are separated by the specified separator. You can also use the optional maxsplit parameter to limit the number of splits that can be made.

The split() method is often used in conjunction with the join() method to manipulate strings in Python. The join() method is used to join a list of strings into a single string, using a specified separator.

Overall, the split() method is a useful tool for any Python developer who needs to manipulate strings in their code. It is easy to use and provides a lot of flexibility in terms of how strings can be broken down and manipulated.


Next TopicPython Strings




Latest Courses