How to Split String in Python?

Python is a very versatile language that has a very large collection of built-in modules and functions. In Python, string variables are manipulated (like splitting and concatenation) using different built-in methods to serve a specific purpose. The split operation allows to break down of the string into multiple substrings. These substrings may be used in further code. 

This write-up will guide you on how to split the string in Python and present a detailed overview of the following aspects:

So let’s get started!

How to Split String in Python?

In Python, the “spilt()” method is used to split the string value into the list by providing the separator value into its parameter. The separator value will depend on the requirement of our code; it will be any value within the string, i.e., whitespaces, comma(,), semicolon, or any character. The syntax of the “split() method” is given below

string.split(Separator, Max-split)
how to handle linux files and direc...

There are two parameters of the “string()” method:

  • Separator parameter: its value will be used to separate the string from the given string into a list. The default value of the “separator” parameter is any whitespace. This parameter is also known as a “delimiter”. The “delimiter” could be any character, special character, etc.
  • Max-split parameter: its value indicates the maximum number of separations that can be done on the given string.

Let’s understand the concept of the “split()” method with different examples:

Example 1: Split Strings Using Default Delimiter

The default value of the delimiter is whitespace. Here, we will use the default delimiter to split strings into multiple substrings:

Code

#split the string without passing any parameters value
string_1 = "Good Morning Everyone"
string_2 = string_1.split()

print(string_2)

In the above code:

  • String value is initialized in a variable named “string_1”.
  • split()” method is used to split all the words of a string into the list of a string and save it into a new variable named “string_2”.
  • There is no parameter value inside the “split()” method: so, it automatically splits the string at “whitespace”.

Output

The output shows that string values have split into three separate lists.

Example 2: Split String Using a Custom Delimiter

The custom delimiter refers to the list of delimiters that are used by most programmers. One can use any of these delimiters to split strings. Before that, ensure that the delimiter must have been used in the string.

Let’s have a look at the following code where the string is being split using the “comma” as a delimiter:

Code

#split the string using comma (,)
string_1 = "hello, Good Morning, My name is Ali"

string_2 = string_1.split(", ")

print(string_2)

In the above code:

  • The variable named “string_1” is initialized with a string value.
  • The “split()” method separator parameter takes a comma “,” with space to separate the string value at this specific parameter.

Output

The output shows that the string is split into lists from the place of specified separators.

Similarly, any character or special character such as “#”, or “$” can be used as a delimiter to split strings.

Example 3: Limit the Split Number

Apart from specifying the delimiter, the string can be split into a limited number of substrings. Let’s understand this using the example below:

Code

#split the string with maximum parameter value (1)
string_1 = "cat#dog#lion#birds"
string_2 = string_1.split("#", 1)
print(string_2)

#split the string with maximum parameter value (3)
string_1 = "cat#dog#lion#birds"
string_2 = string_1.split("#", 3)
print(string_2)

In the above output:

  • The string value is initialized in a variable named “string_1”.
  • The “split()” method takes max limit parameter values “1” and “3”.
  • In the first part of the code, the max limit value is “1” which states that the “split()” method stops the splitting process after encountering the delimiter(#) 1(one) time. Similarly, in the second part of the code, the max limit is “3”.

Output

It can be seen from the output that the first string was divided into two parts as the limit was one. Similarly, the string in the second part is divided into four parts because the limit was 3.

That’s all from this guide!

Conclusion

In Python, the “split()” method is used to split the string according to the value provided by the “separator” parameter and “max split” parameter. The max split parameter value holds the max number of occurrences that will occur on the given string. The “split()” method returns the value in the form of a list of strings, and each list can be accessed using the index slicing method. This post briefly explained the working of the “split()” method with appropriate examples.