duck.utils.string¶
String utilities module providing a variety of functions for common string manipulation tasks.
Module Contents¶
Functions¶
Check if two strings are anagrams of each other. |
|
Converts a string from camelCase to snake_case. |
|
Capitalizes the first letter of each word in the string. |
|
Removes unwanted characters (e.g., spaces or specific symbols) from the string. |
|
Returns the number of words in a string. |
|
Return all starting indices where the target substring occurs in the text (uses regex). |
|
Replace all occurrences of a target substring with a replacement string. |
|
Returns the indices of all occurrences of a substring in the string. |
|
Decode a base64 encoded string. |
|
Check if a given string follows CamelCase (UpperCamelCase) or lowerCamelCase. |
|
Checks if a string follows kebab-case. |
|
Returns True if the string is a palindrome, otherwise False. |
|
Checks if a string follows PascalCase (UpperCamelCase). |
|
Checks if a string follows SCREAMING_SNAKE_CASE. |
|
Checks if a string follows snake_case. |
|
Checks if a string follows Train-Case (Capitalized Kebab Case). |
|
Justify text to the left, right, or center to fit a specified width. |
|
Pad a string to a specific total length. |
|
Remove all non-alphanumeric characters (keeps letters and numbers) using Regex. |
|
Remove extra spaces from a string, leaving only one space between words. |
|
Removes all characters except alphanumeric (letters and digits). |
|
Remove punctuation from a string. |
|
Replaces multiple substrings with the provided replacements. |
|
Reverse the order of words in a string. |
|
Truncates the given text if it exceeds the specified character cap, ensuring that the truncation occurs at the last complete word or grammar boundary defined by the provided list of grammar elements. |
|
Converts a string from snake_case to camelCase. |
|
Compute the similarity between two strings using Levenshtein distance. |
|
Encode a string in base64. |
|
Convert a string to camelCase. |
|
Convert a string to kebab-case. |
|
Converts a PascalCase or camelCase string into a spaced string. Example: ‘SomeItem’ -> ‘Some Item’ |
|
Change the case of the string based on a condition. |
|
Wraps the text to the specified width. Can align the text to left, center, or right. |
|
Wrap words at a specified line length. |
API¶
- duck.utils.string.are_anagrams(str1: str, str2: str) bool[source]¶
Check if two strings are anagrams of each other.
- duck.utils.string.camel_to_snake_case(text: str) str[source]¶
Converts a string from camelCase to snake_case.
- duck.utils.string.capitalize_words(text: str) str[source]¶
Capitalizes the first letter of each word in the string.
- duck.utils.string.clean_string(text: str, remove_chars: list[str] = None) str[source]¶
Removes unwanted characters (e.g., spaces or specific symbols) from the string.
- duck.utils.string.find_all_occurrences(text: str, target: str) List[int][source]¶
Return all starting indices where the target substring occurs in the text (uses regex).
- duck.utils.string.find_and_replace(text: str, target: str, replacement: str) str[source]¶
Replace all occurrences of a target substring with a replacement string.
- duck.utils.string.find_occurrences(text: str, substring: str) list[source]¶
Returns the indices of all occurrences of a substring in the string.
- duck.utils.string.is_camel_case(s: str) bool[source]¶
Check if a given string follows CamelCase (UpperCamelCase) or lowerCamelCase.
- Parameters:
s – The input string to check.
- Returns:
True if the string is in CamelCase or lowerCamelCase, False otherwise.
- Return type:
bool
- duck.utils.string.is_palindrome(text: str) bool[source]¶
Returns True if the string is a palindrome, otherwise False.
- duck.utils.string.is_pascal_case(s: str) bool[source]¶
Checks if a string follows PascalCase (UpperCamelCase).
- duck.utils.string.is_screaming_snake_case(s: str) bool[source]¶
Checks if a string follows SCREAMING_SNAKE_CASE.
- duck.utils.string.is_train_case(s: str) bool[source]¶
Checks if a string follows Train-Case (Capitalized Kebab Case).
- duck.utils.string.justify_text(text: str, width: int, direction: str = 'left') str[source]¶
Justify text to the left, right, or center to fit a specified width.
- Parameters:
text – The input text.
width – The target width to justify to.
direction – The direction to justify (“left”, “right”, or “center”).
- Returns:
The text formatted to the specified width.
- Return type:
str
- duck.utils.string.pad_text(text: str, total_length: int, padding_char: str = ' ') str[source]¶
Pad a string to a specific total length.
- duck.utils.string.re_remove_non_alphanumeric(text: str) str[source]¶
Remove all non-alphanumeric characters (keeps letters and numbers) using Regex.
- duck.utils.string.remove_extra_spaces(text: str) str[source]¶
Remove extra spaces from a string, leaving only one space between words.
- duck.utils.string.remove_non_alphanumeric(text: str) str[source]¶
Removes all characters except alphanumeric (letters and digits).
- duck.utils.string.replace_multiple(text: str, replacements: dict) str[source]¶
Replaces multiple substrings with the provided replacements.
Example: {“old1”: “new1”, “old2”: “new2”}
- duck.utils.string.reverse_words_in_sentence(text: str) str[source]¶
Reverse the order of words in a string.
- duck.utils.string.smart_truncate(text: str, cap: int, grammar: list = None, suffix: str = '...') str[source]¶
Truncates the given text if it exceeds the specified character cap, ensuring that the truncation occurs at the last complete word or grammar boundary defined by the provided list of grammar elements.
- Parameters:
text – The string to be truncated. Can be a regular text or a file path.
cap – The maximum allowed length of the string.
grammar – A list of grammar elements such as words, punctuation, etc. These will be used to define truncation boundaries.
suffix – The suffix to append if truncation occurs. Default is ‘…’.
- Returns:
The truncated string with the specified suffix appended if truncation occurs, or the original string if no truncation is needed.
- Return type:
str
… rubric:: Example
smart_truncate(“This is a long sentence that might need truncation.”, 30, grammar=[’ ‘, ‘.’])
Returns: “This is a long…”
- duck.utils.string.snake_to_camel_case(text: str) str[source]¶
Converts a string from snake_case to camelCase.
- duck.utils.string.string_similarity(str1: str, str2: str) float[source]¶
Compute the similarity between two strings using Levenshtein distance.
- duck.utils.string.to_camel_case(text: str, separator: str = '_') str[source]¶
Convert a string to camelCase.
- duck.utils.string.to_spaced_camel_case(value: str) str[source]¶
Converts a PascalCase or camelCase string into a spaced string. Example: ‘SomeItem’ -> ‘Some Item’
- duck.utils.string.transform_case_based_on_condition(text: str, condition: bool) str[source]¶
Change the case of the string based on a condition.
- Parameters:
text – The input string.
condition – If True, converts text to uppercase; else lowercase.
- Returns:
The transformed string.
- Return type:
str
- duck.utils.string.wrap_text(text: str, width: int, align: str = 'left') str[source]¶
Wraps the text to the specified width. Can align the text to left, center, or right.
- Parameters:
text – The text to wrap.
width – The width at which to wrap the text.
align – The alignment type (“left”, “center”, “right”). Default is “left”.
- Returns:
The wrapped text, aligned as requested.
- Return type:
str