Skip to content

textcase.convert()#

textcase.convert #

convert(text, case, boundaries=DEFAULT_BOUNDARIES)

Convert the given text to the specified case format.

PARAMETER DESCRIPTION
text

The input string to be converted.

TYPE: str

case

The case format to convert the text to, which should be an instance of case.Case.

TYPE: Case

boundaries

A collection of Boundary instances that define the split conditions.

TYPE: Iterable[Boundary] DEFAULT: DEFAULT_BOUNDARIES

RETURNS DESCRIPTION
str

The input string converted to the specified case format.

TYPE: str

Examples:

>>> assert convert("2020-04-16_my_cat_cali", case.SNAKE) == "2020_04_16_my_cat_cali"
>>> assert convert("my_Cat-CALI", case.CAMEL) == "myCatCali"
Source code in textcase/__init__.py
def convert(text: str, case: case.Case, boundaries: Iterable[boundary.Boundary] = boundary.DEFAULT_BOUNDARIES) -> str:
    """Convert the given text to the specified case format.

    Args:
        text (str): The input string to be converted.
        case (case.Case): The case format to convert the text to, which should be an instance of `case.Case`.
        boundaries (Iterable[Boundary], optional): A collection of Boundary instances that define the split conditions.

    Returns:
        str: The input string converted to the specified case format.

    Examples:
        >>> assert convert("2020-04-16_my_cat_cali", case.SNAKE) == "2020_04_16_my_cat_cali"
        >>> assert convert("my_Cat-CALI", case.CAMEL) == "myCatCali"
    """
    return case.delimiter.join(case.pattern(boundary.split(text, boundaries)))