Python - Detecting a pattern with the split() function

The following examples show how to detect a pattern and split a string using the split function in Python. The function split() returns a list of all the words in the string as a result.

Split by space

The function split() defaults to the empty or space character as the delimiter.

alphabet = "a b c d e f"
tstr = alphabet.split()

for c in tstr:
print c
Runtime:

a
b
c
d
e
f
The function split() also takes as a parameter the maximum number of detections of a character or pattern as shown in the example below:

alphabet = "a b c d e f"
tstr = alphabet.split(" ", 3)

for c in tstr:
print c
Runtime:

a
b
c
d e f

Example 2: Extracting the domain name

s = 'exemple.domaine.net'
i = s.split('.', 1)
nom_de_domaine = i[1]
print nom_de_domaine
Runtime:

domaine.net
References:
Python String split() Method