split() vs. partition() in Python Strings

What’s the difference?

Indhumathy Chelliah
Sep 17 · 5 min read
Image for post
Image for post
Photo by Grant Ritchie on Unsplash

split() vs. partition()

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

split()

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

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()

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()

Example 1: Splitting the string based on line breaks

colors="red\norange\nyellow\npurple"
print (colors.splitlines())
#Output:['red', 'orange', 'yellow', 'purple']

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

colors="red\norange\nyellow\npurple"
print (colors.splitlines(keepends=True))
#Output:['red\n', 'orange\n', 'yellow\n', 'purple']

Example 3: split() vs splitlines()

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

partition()

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()

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()

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

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

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

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

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 for post
Image for post
Image source: author

Conclusion

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store