How to use Split in Python

The split() method in Python operates by partitioning a string or line into distinct words, utilizing a designated delimiter string as the basis for segmentation. This method yields a collection of one or more novel strings, each of which corresponds to a distinct substring created by the segmentation process. As a result, the substrings are systematically collated and presented within the context of a list datatype, facilitating efficient data organization and manipulation.

Syntax
string.split(separator, max)
  1. Separator:The is a delimiter. The string splits at this specified separator. If is not provided then any white space is a separator.
  2. Maxsplit: It is a number, which tells us to split the string into maximum of provided number of times. If it is not provided then there is no limit.
  3. Return:The split() breaks the string at the separator and returns a list of strings.

In the absence of a explicitly defined separator when invoking the function, the default behavior of the split() function in Python involves utilizing whitespace as the delimiter. In more straightforward terms, the separator signifies a predetermined character that functions as an intermediary between individual variables during the segmentation process. The functionality of the split() function when provided with an empty string hinges on the value assigned to the "sep" parameter. If "sep" is unspecified or set to None, the outcome will be an empty list. Conversely, if "sep" is specified as any string value, the result will consist of a list housing a single element, which is an empty string.

Splitting String by space

The split() method functions without a specified argument, leading to segmentation based on whitespace.

example
str = "This is a test" print(str.split())
output
['This', 'is', 'a', 'test']

Splitting on first occurrence

In the following example, it will Split by first 2 whitespace only.

example
str = "This is a test" print(str.split(" ",2))
output
['This', 'is', 'a test']

Splitting lines from a text file in Python

The following Python program reading a text file and splitting it into single words in python

example
with open("my_file.txt", "r") as my_file: for line in my_file: str = line.split() print(str)

Splitting String by newline(\n)

str = "This \nis \na \ntest" print(str) print("\nAfter Split\n") print(str.split())
output
This is a test After Split ['This', 'is', 'a', 'test']

Splitting String by tab(\t)

str = "This \tis \ta \ttest" print(str) print("\nAfter Split\n") print(str.split())
output
This is a test After Split ['This', 'is', 'a', 'test']

Splitting String by comma(,)

str = "This,is,a,test" print(str.split(","))
output
['This', 'is', 'a', 'test']

Split string with multiple delimiters

In this case Python uses Regular Expression.

example
import re str = "This,isa;test" print(re.split(",;",str))
output
['This', 'is', 'a', 'test']

Split a string into a list

The following Python program split a string to a List.

example
str = "This is a test" lst = str.split() for st in lst: print(st)
output
This is a test

maxsplit parameter

Split the string into a list with max 2 items

numbers = "one two three four five six" result = numbers.split(" ",2) print (result)
output
['one', 'two', 'three four five six']

In the above program maxsplit is 2, the first two string are split and rest of them are in a same string.

Split a string into array of characters

characters = "abcdef" result = list(characters) print (result)
output
['a', 'b', 'c', 'd', 'e', 'f']

Python split() using substring

Extact a string after a specific substring.


How to Extact a string after a specific substring in python

In the above example, you can see the split() function return next part of a string using a specific substring.


extract previous part string of a specific substring

Here, you can see the split() function return the previous part of the string using a specific substring.

Conclusion

The split() method is employed to divide a string into distinct segments based on a designated separator. By default, if no separator is specified, whitespace is used as the separator. This method facilitates effective string segmentation, aiding in data processing and manipulation.