Remember
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.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
text = 'tea:milk'
result = text.partition(":")
print(result)Top-level scope
'tea:milk'Object 2 · tuple('tea', ':', 'milk')partition() splits at the first separator and returns a three-part tuple: before, separator, after.
(no output)
What do you expect this program to print?
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')Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
text = 'tea:milk'
result = text.partition(":")
print(result)Top-level scope
'tea:milk'Object 2 · tuple('tea', ':', 'milk')The example has finished. Compare the final output with the values and references above.
('tea', ':', 'milk')
What do you expect this program to print?
text = 'tea:milk'
result = text.partition(":")
print(result)partition() MethodThe 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.
string.partition(separator)
separator – The string to search for (must not be empty).(before_separator, separator, after_separator)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 aftertext = "learnpython"
result = text.partition(" ")
print(result)
('learnpython', '', '')
The separator (a space) was not found, so the whole string is returned as the first element.
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
partition() splits at the first occurrence only.split() can split multiple times and returns a list.ValueErrorIf you're asked to extract a prefix or postfix around a delimiter in a string, partition() is the fastest and cleanest method to use.
partition() splits a string into 3 parts using a separator.(before, separator, after)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
Connect what you noticed in the example with the idea below.
partition() splits at the first separator and returns a three-part tuple: before, separator, after.
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.