String Methods
partition()
Split text once at the first separator and keep the separator in the result.
str.partition(sep)
str.partition() splits the string at the first occurrence of sep and returns a 3-tuple: (head, sep, tail).
If sep is not found, returns (string, '', '').
Parameters
| Name | Description |
|---|---|
| sep | The separator text to find; it cannot be empty. |
Returns
A three-item tuple: text before the separator, the separator, and text after it.
Try it
Run it and change it
text = "name=value"
print(text.partition("="))
Worth knowing
- Always returns 3 elements: Guarantees a 3-tuple output, making it much easier to unpack safely than
split().