duck.utils.string

String utilities module providing a variety of functions for common string manipulation tasks.

Module Contents

Functions

are_anagrams

Check if two strings are anagrams of each other.

camel_to_snake_case

Converts a string from camelCase to snake_case.

capitalize_words

Capitalizes the first letter of each word in the string.

clean_string

Removes unwanted characters (e.g., spaces or specific symbols) from the string.

count_words

Returns the number of words in a string.

find_all_occurrences

Return all starting indices where the target substring occurs in the text (uses regex).

find_and_replace

Replace all occurrences of a target substring with a replacement string.

find_occurrences

Returns the indices of all occurrences of a substring in the string.

from_base64

Decode a base64 encoded string.

is_camel_case

Check if a given string follows CamelCase (UpperCamelCase) or lowerCamelCase.

is_kebab_case

Checks if a string follows kebab-case.

is_palindrome

Returns True if the string is a palindrome, otherwise False.

is_pascal_case

Checks if a string follows PascalCase (UpperCamelCase).

is_screaming_snake_case

Checks if a string follows SCREAMING_SNAKE_CASE.

is_snake_case

Checks if a string follows snake_case.

is_train_case

Checks if a string follows Train-Case (Capitalized Kebab Case).

justify_text

Justify text to the left, right, or center to fit a specified width.

pad_text

Pad a string to a specific total length.

re_remove_non_alphanumeric

Remove all non-alphanumeric characters (keeps letters and numbers) using Regex.

remove_extra_spaces

Remove extra spaces from a string, leaving only one space between words.

remove_non_alphanumeric

Removes all characters except alphanumeric (letters and digits).

remove_punctuation

Remove punctuation from a string.

replace_multiple

Replaces multiple substrings with the provided replacements.

reverse_words_in_sentence

Reverse the order of words in a string.

smart_truncate

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.

snake_to_camel_case

Converts a string from snake_case to camelCase.

string_similarity

Compute the similarity between two strings using Levenshtein distance.

to_base64

Encode a string in base64.

to_camel_case

Convert a string to camelCase.

to_kebab_case

Convert a string to kebab-case.

to_spaced_camel_case

Converts a PascalCase or camelCase string into a spaced string. Example: ‘SomeItem’ -> ‘Some Item’

transform_case_based_on_condition

Change the case of the string based on a condition.

wrap_text

Wraps the text to the specified width. Can align the text to left, center, or right.

wrap_words

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.count_words(text: str) int[source]

Returns the number of words in a 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.from_base64(encoded_text: str) str[source]

Decode a base64 encoded 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_kebab_case(s: str) bool[source]

Checks if a string follows kebab-case.

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_snake_case(s: str) bool[source]

Checks if a string follows 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.remove_punctuation(text: str) str[source]

Remove punctuation from a string.

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_base64(text: str) str[source]

Encode a string in base64.

duck.utils.string.to_camel_case(text: str, separator: str = '_') str[source]

Convert a string to camelCase.

duck.utils.string.to_kebab_case(text: str) str[source]

Convert a string to kebab-case.

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

duck.utils.string.wrap_words(text: str, line_length: int) str[source]

Wrap words at a specified line length.