split() vs. partition() in Python Strings

What’s the difference?

Photo by Grant Ritchie on Unsplash

split() vs. partition()

In Python, we can split the string by using the following methods. Let’s look at these methods in detail.

1. split()
2. rsplit()
3. splitlines()
4. partition()
5. rpartition()
6. re.split()
7. Differences between split() and partition()
8. Conclusion
9. Resources

split()

“Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made).”

Python docs

str.split(sep=None,maxsplit=-1)

Return type → List

Example 1: If a sep (delimiter) isn’t mentioned, it’ll split the string based on the whitespaces in the string

a="Hello Python"
print (a.split())
#Output:['Hello', 'Python']

Example 2: If a sep (delimiter) is mentioned, it’ll split based on the occurrences of the delimiter

colors='red-green-blue-yellow'
print (colors.split("-"))
#Output:['red', 'green', 'blue', 'yellow']

Example 3

a="three times by three makes nine"
print (a.split(sep="three"))
#Output:['', ' times by ', ' makes nine']

Example 4: A maxsplit is mentioned

If maxsplit is mentioned as 1, it’ll split on the first occurrence only.
If maxsplit is given as 2, it’ll split on the first two occurrences only.

colors="red-orange-yellow-purple"

print (colors.split("-",maxsplit=1))
#Output:['red', 'orange-yellow-purple']

print (colors.split("-",maxsplit=2))
#Output:['red', 'orange', 'yellow-purple']

Example 5: If a sep (delimiter) isn’t present in the string, it won’t split the string — it’ll return a list containing the string itself

s="HelloPython"
print (s.split())
#Output:['HelloPython']

Example 6: A sep (delimiter) can contain multiple characters also — they’re grouped together

colors="red<>green<>yellow<>blue<orange"
print(colors.split("<>"))
#Output:['red', 'green', 'yellow', 'blue<orange']

rsplit()

“Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done, the rightmost ones. If sep is not specified or None, any whitespace string is a separator. Except for splitting from the right,rsplit()behaves like split().”

Python docs

str.rsplit(sep=None,maxsplit=-1)

Return type → List

Example 1: If no sep (delimiter) is mentioned, it’ll split the string based on the whitespaces in the string — same as split() only

a="Hello Python"
print (a.rsplit())
#Output:['Hello', 'Python']

Example 2: If a maxsplit is mentioned as 1, it’ll split on the first occurrence from the right; if a maxsplit is given as 2, it’ll split on the first two occurrences from the right

colors="red-orange-yellow-purple"

print (colors.rsplit("-",maxsplit=1))
#Output:['red-orange-yellow', 'purple']

print (colors.rsplit("-",maxsplit=2))
#Output:['red-orange', 'yellow', 'purple']

splitlines()

“Return a list of the lines in the string, breaking at line boundaries. Line breaks are not included in the resulting list unless keepends is given and true.”

Python docs

str.splitlines([keepends])

Example 1: Splitting the string based on line breaks

colors="rednorangenyellownpurple"
print (colors.splitlines())
#Output:['red', 'orange', 'yellow', 'purple']

Example 2: Line breaks are included by mentioning keepends=True

colors="rednorangenyellownpurple"
print (colors.splitlines(keepends=True))
#Output:['redn', 'orangen', 'yellown', 'purple']

Example 3: split() vs splitlines()

colors="rednorangenyellownpurplen"
print (colors.splitlines())
#Output:['red', 'orange', 'yellow', 'purple']
print(colors.split("n"))
#Output:['red', 'orange', 'yellow', 'purple', '']

partition()

“Split the string at the first occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing the string itself, followed by two empty strings.”

Python docs

str.partition(sep)

Return type → Tuple

Example 1: Splits the string at the first occurrence of a sep (delimiter)

colors="red-orange-yellow-purple"
print (colors.partition("-"))
#Output:('red', '-', 'orange-yellow-purple')

Example 2: The sep is given as a space

s="Hello Python"
print (s.partition(" "))
#Output:('Hello', ' ', 'Python')

Example 3: If a sep isn’t mentioned, it’ll raise a TypeError

s="Hello Python"
print (s.partition())
#Output:TypeError: partition() takes exactly one argument (0 given)

Example 4: If a sep isn’t found in the string, it’ll return a 3-tuple containing the string itself, followed by two empty strings

s="HelloPython"
print (s.partition(" "))
#Output:('HelloPython', '', '')

rpartition()

“Split the string at the last occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing two empty strings, followed by the string itself.”

Python docs

str.rpartition(sep)

Example 1: Splits the string at the last occurrence of a sep (delimiter)

colors="red-orange-yellow-purple"
print (colors.rpartition("-"))
#Output:('red-orange-yellow', '-', 'purple')

Example 2: If a sep isn’t found in the string, a 3-tuple will return containing two empty strings, followed by the string itself

s="HelloPython"
print (s.rpartition(" "))
#Output:('', '', 'HelloPython')

Example 3: If a sep isn’t mentioned, it’ll raise a TypeError

s="Hello Python"
print (s.rpartition())
#Output:TypeError: rpartition() takes exactly one argument (0 given)

re.split()

“Split string by the occurrences of pattern. If capturing parentheses are used in the pattern, then the text of all groups in the pattern are also returned as part of the resulting list. If maxsplit is nonzero, at most maxsplit splits occur, and the remainder of the string is returned as the final element of the list.”

Python docs

re.split(pattern, string, maxsplit=0, flags=0)

Return type → List

Example 1: Splitting the string by giving a space as a delimiter

s="Hello Python"
import re
print (re.split(" ",s))
#Output:['Hello', 'Python']

Example 2:Splitting the string with a single delimiter

colors="red-orange-yellow-purple"
import re
print (re.split("-",colors))
#Output:['red', 'orange', 'yellow', 'purple']

Example 3: Spitting the string with multiple delimiters using re.split

re.split(‘[&$-]’,colors

[&$-][]is used to indicate a set of characters. Multiple delimiters are given within the square bracket. re.split() will split the string if any of the delimiters mentioned are found.

colors="red&orange-yellow$purple"
import re
print (re.split('[&$-]',colors))
#Output:['red', 'orange', 'yellow', 'purple']

Example 4: Splitting the string at the occurrences of any character other than alphanumerics (a-z, A-Z, 0–9) and underscore

re.split(‘W’,colors)

W → Matches all characters except a-z, A-Z, and 0-9

colors="red%yellow,blue!orange@purple"
import re
print (re.split('W',colors))
#Output:['red', 'yellow', 'blue', 'orange', 'purple']

Example 5: Splitting the string at the occurrence of any character other than numbers

re.split(‘D’,num)

D → Matches all characters except 0-9

num="1,2%3&4!5@6"
import re
print (re.split('D',num))
#Output:['1', '2', '3', '4', '5', '6']

Example 6: A maxsplit is given

If a maxsplit is mentioned as 1, it’ll split on the first occurrence only. If a maxsplit is given as 2, it’ll split on the first two occurrences only.

colors="red-orange-yellow-purple"
import re
print (re.split("-",colors,maxsplit=1))
#Output:['red', 'orange-yellow-purple']

print (re.split("-",colors,maxsplit=2))
#Output:['red', 'orange', 'yellow-purple']

Differences Between split() and partition()

 

Image source: author

Conclusion

  • str.split() → It’ll split the string by each occurrence of the delimiter (sep)
  • str.partition() → It’ll split the string on the first occurrences of the delimiter (sep)
  • str.rpartition() → It’ll split the string on the last occurrences
     of the delimiter (sep)
  • re.split() → It’ll split the string on the occurrences of the pattern. Multiple delimiters can be used to split the string.

My other blogs related to string methods

Remove Whitespaces from Strings in Python

5 Ways to Find the Index of a Substring in Python

5 Different Ways to Remove Specific Characters From a String in Python

Different Ways to Replace Occurences of a Substring in Python Strings


Resources (Python Documentation)

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

Buy Me a CoffeeBuy Me a CoffeeBuy Me a Coffee

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s