YouTip LogoYouTip

Att String Rpartition

## Python String rpartition() Method The `rpartition()` method splits a string at the last occurrence of a specified separator. It searches for the separator starting from the end (the right side) of the string. Unlike standard splitting methods that return a list of arbitrary length, `rpartition()` always returns a 3-element tuple containing: 1. The substring before the separator. 2. The separator itself. 3. The substring after the separator. --- ## Syntax ```python str.rpartition(separator) ``` ### Parameters * **`separator`** *(required)*: The string/character to search for and split on. ### Return Value Returns a 3-element tuple `(before, separator, after)`: * **If the separator is found**: The tuple contains the substring before the last occurrence of the separator, the separator itself, and the substring after it. * **If the separator is not found**: The tuple contains two empty strings and the original string: `('', '', original_string)`. --- ## Code Examples ### Example 1: Basic Usage (Separator Found) In this example, we split a domain name using the dot (`.`) as a separator. Since `rpartition()` starts searching from the right, it splits at the last dot. ```python # Define the target string url = "www.youtip.co.uk" # Split from the right using "." result = url.rpartition(".") print("Result Tuple:", result) print("Before last dot:", result) print("Separator:", result) print("After last dot:", result) ``` **Output:** ```text Result Tuple: ('www.youtip.co', '.', 'uk') Before last dot: www.youtip.co Separator: . After last dot: uk ``` --- ### Example 2: Separator Not Found If the specified separator is not present in the string, `rpartition()` returns a tuple where the first two elements are empty strings, and the third element is the original string. ```python text = "Hello World" # Search for a separator that does not exist result = text.rpartition("@") print(result) ``` **Output:** ```text ('', '', 'Hello World') ``` --- ### Example 3: Comparing `partition()` vs `rpartition()` To understand the difference between searching from the left (`partition()`) and searching from the right (`rpartition()`), look at the following comparison: ```python filepath = "usr/local/bin/python3" # partition() splits at the FIRST occurrence of "/" print("partition(): ", filepath.partition("/")) # rpartition() splits at the LAST occurrence of "/" print("rpartition():", filepath.rpartition("/")) ``` **Output:** ```text partition(): ('usr', '/', 'local/bin/python3') rpartition(): ('usr/local/bin', '/', 'python3') ``` --- ## Considerations & Best Practices * **Extracting File Extensions and Paths**: `rpartition()` is highly efficient for parsing file paths and URLs. For example, `filepath.rpartition('/')` easily separates the directory path from the filename. * **Empty Separator Error**: The `separator` argument cannot be an empty string. If you pass an empty string (`""`), Python will raise a `ValueError: empty separator`. * **Type Safety**: The separator must be a string. Passing other data types (like integers or lists) will raise a `TypeError`.
← Prop Win SessionstorageJsref From β†’