Python partition() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

partition() looks for a separator, and cuts the string at its first appearance. It always returns a tuple of exactly three items: the part before, the separator itself, and the part after.

If the separator is not found, you still get three items: the whole string, then two empty strings. That fixed size makes it easy to unpack into three variables, and the middle item tells you whether the separator was found. The original string is not changed.

text.partition() is a method of the built-in str type: call it on a string with dot notation. No import is needed, and it never changes the original string.

Syntax

Pass the separator to look for. It is required, and it must be a string.

python
before, sep, after = text.partition(sep)
CallWhat it does
"key=value".partition("=")Returns ('key', '=', 'value'). The text is cut at the equals sign.
"key=a=b".partition("=")Returns ('key', '=', 'a=b'). Only the first equals sign is used.
"novalue".partition("=")Returns ('novalue', '', ''). The separator is missing, so the whole text is the first item.

To cut at the last separator use rpartition(). To get a list and cut at every separator, use split(). To only find where the separator is, use find().

Library declarations
python
str.partition(sep) -> tuple
# returns: (before, sep, after), a tuple of three strings

Parameters

ParameterDescription
sepRequired. The text to split at. It must be a non-empty string, otherwise ValueError is raised.

Return Value

Return ValueDescription
tupleA tuple of three strings: the part before the first separator, the separator, and the part after. If the separator is not found, it is (text, '', '').

Python Examples

Example 1 — Split a Name and a Value

Unpack the three items into variables, then look at the tuple itself.

python
pair = "name=Ada"
head, sep, tail = pair.partition("=")

print(head)
print(sep)
print(tail)
print(pair.partition("="))

How It Works

  1. Three items. The result always has a before part, the separator, and an after part, so it can be unpacked into three variables.
  2. The separator is kept. The middle item is the equals sign itself.
  3. A tuple. Printing the call shows the three items together, in parentheses.

Example 2 — Read Settings With and Without a Value

Use the middle item as a flag that tells whether the separator was found.

python
for text in ["color=red", "debug", "path=a=b"]:
    key, sep, value = text.partition("=")
    if sep:
        print(key, "->", value)
    else:
        print(key, "has no value")

How It Works

  1. The middle item is a flag. A found separator is a non-empty string, which is true in an if. A missing one is empty, which is false.
  2. No error for a missing separator. "debug" has no equals sign, but partition() still returns three items.
  3. Only the first separator splits. path=a=b gives the value a=b, because the second equals sign stays in the last item.

Example 3 — Find the User and the Domain

Cut at the at sign, then cut the domain again at its first dot.

python
email = "[email protected]"
user, at, domain = email.partition("@")

print(user)
print(domain)
print(domain.partition(".")[0])

How It Works

  1. Cut at the at sign. The user name goes into user, and the rest goes into domain.
  2. An unused separator. The variable at holds the @ sign, and it is not needed any more.
  3. Chain a second cut. domain.partition(".")[0] takes the part before the first dot, which is mail.

Frequently Asked Questions

It splits a string at the first occurrence of a separator and returns a tuple of three strings: the part before, the separator, and the part after.
It returns a tuple with the whole string first, followed by two empty strings.
partition() cuts once and always returns three items, including the separator. split() cuts at every separator by default and returns a list without the separators.
partition() uses the first separator, and rpartition() uses the last one. Both return three items.
Yes. The separator can be any non-empty string, for example "text".partition("::").

Next: removeprefix()

Return a copy without the given prefix, if the string starts with it. Python 3.9+.

removeprefix() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful