Python Reference Library →

Python String partition() Method – Split into Three Parts

Let’s look at string partition() method – split into three parts

  1. Notice

    partition() splits at the first separator and returns a three-part tuple: before, separator, after.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = 'tea:milk'
result = text.partition(":")
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'tea:milk'Object 2 · tuple('tea', ':', 'milk')
This step

partition() splits at the first separator and returns a three-part tuple: before, separator, after.

Picture string partition() method – split into three parts

partition() splits at the first separator and returns a three-part tuple: before, separator, after.

Image brief

Alt text
A diagram for Python String partition() Method – Split into Three Parts. Instructions: text = 'tea:milk' result = text.partition(":") print(result). Displayed result: ('tea', ':', 'milk'). The final example bindings are text --> object 1 (str); result --> object 2 (tuple). object 1: 'tea:milk'; object 2: ('tea', ':', 'milk')
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String partition() Method – Split into Three Parts". Teaching focus: partition() splits at the first separator and returns a three-part tuple: before, separator, after. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = 'tea:milk' result = text.partition(":") print(result) | v DISPLAYED RESULT ('tea', ':', 'milk') FINAL BINDINGS text --> object 1 (str) result --> object 2 (tuple) object 1: 'tea:milk' object 2: ('tea', ':', 'milk') Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python String partition() Method – Split into Three Parts. Instructions: text = 'tea:milk' result = text.partition(":") print(result). Displayed result: ('tea', ':', 'milk'). The final example bindings are text --> object 1 (str); result --> object 2 (tuple). object 1: 'tea:milk'; object 2: ('tea', ':', 'milk')

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = 'tea:milk'
result = text.partition(":")
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'tea:milk'Object 2 · tuple('tea', ':', 'milk')
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
('tea', ':', 'milk')

What do you expect this program to print?

What does this example display?

text = 'tea:milk'
result = text.partition(":")
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

('tea', ':', 'milk')

Follow the printed values in order. The output is: ('tea', ':', 'milk')

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

partition() splits at the first separator and returns a three-part tuple: before, separator, after.

partition() splits at the first separator and returns a three-part tuple: before, separator, after.

More examples and reference

Python String partition() Method

The partition() method in Python is used to split a string into three parts using a specified separator. It always returns a tuple of 3 elements.

Syntax

string.partition(separator)

Parameters:

  • separator – The string to search for (must not be empty).

Returns:

  • A tuple of 3 parts: (before_separator, separator, after_separator)
  • If the separator is not found, the first item will be the entire string, and the second and third will be empty strings.

Example: Basic Usage

text = "python-is-awesome"
result = text.partition("-")
print(result)
('python', '-', 'is-awesome')

Why? Because:

  • 'python' comes before the first '-'
  • '-' is the separator
  • 'is-awesome' comes after

Example: Separator Not Found

text = "learnpython"
result = text.partition(" ")
print(result)
('learnpython', '', '')

The separator (a space) was not found, so the whole string is returned as the first element.

Real-world Use Case

You can use partition() when you want to break a string into 2 parts at the first occurrence of a keyword or symbol. For example:

email = "user@example.com"
username, sep, domain = email.partition("@")
print("Username:", username)
print("Domain:", domain)
Username: user
Domain: example.com

Comparison with split()

  • partition() splits at the first occurrence only.
  • split() can split multiple times and returns a list.

Common Mistakes

  • Using an empty string as the separator → causes a ValueError
  • Expecting more than 3 values in the returned tuple

Interview Tip

If you're asked to extract a prefix or postfix around a delimiter in a string, partition() is the fastest and cleanest method to use.

Summary

  • partition() splits a string into 3 parts using a separator.
  • Returns a tuple: (before, separator, after)
  • Helpful for quick parsing and string analysis.

Practice Problem

Write a Python program that reads a string like "title:Introduction to Python" and splits it into label and value using partition().

text = "title:Introduction to Python"
label, sep, value = text.partition(":")
print("Label:", label)
print("Value:", value)
Label: title
Value: Introduction to Python

What to remember

Connect what you noticed in the example with the idea below.

Remember

partition() splits at the first separator and returns a three-part tuple: before, separator, after.

Watch out for this idea

partition() returns a list of every separated field.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →