Skip to content

Strings

Strings class#

Modifies the strings to the desired format.

latinise(string: str) -> str #

Removes special unicode characters from the string.

Parameters:

Name Type Description Default
string str

The string to transliterate special unicode characters into latin characters.

required

Returns:

Type Description
str

The string with special unicode characters transliterated.

Examples:

>>> latinise("ピカチュウ")
'pikachiyuu'
Source code in stringmatch/strings.py
def latinise(self, string: str) -> str:
    """Removes special unicode characters from the string.

    Parameters
    ----------
    string : str
        The string to transliterate special unicode characters into latin characters.

    Returns
    -------
    str
        The string with special unicode characters transliterated.

    Examples
    --------
    >>> latinise("ピカチュウ")
    'pikachiyuu'
    """
    return unidecode(string)

remove_punctuation(string: str) -> str #

Removes punctuation from a string.

Parameters:

Name Type Description Default
string str

The string to remove punctuation from.

required

Returns:

Type Description
str

The string with punctuation removed.

Examples:

>>> remove_punctuation("...Hello!!!")
'Hello'
Source code in stringmatch/strings.py
def remove_punctuation(self, string: str) -> str:
    """Removes punctuation from a string.

    Parameters
    ----------
    string : str
        The string to remove punctuation from.

    Returns
    -------
    str
        The string with punctuation removed.

    Examples
    --------
    >>> remove_punctuation("...Hello!!!")
    'Hello'
    """
    return "".join(
        c for c in string if c not in "!\"#'()*+,-./:;<=>?[]^_`{|}~’„“»«"
    )

alphanumeric(string: str) -> str #

Removes all non-latin letters from the string. Does also keep numbers. A more extreme version of remove_punctuation().

Parameters:

Name Type Description Default
string str

The string to remove non-latin letters from.

required

Returns:

Type Description
str

The string with non-latin letters removed.

Examples:

>>> alphanumeric("What? À special word!")
'What  special word'
Source code in stringmatch/strings.py
def alphanumeric(self, string: str) -> str:
    """Removes all non-latin letters from the string.
    Does also keep numbers.
    A more extreme version of remove_punctuation().

    Parameters
    ----------
    string : str
        The string to remove non-latin letters from.

    Returns
    -------
    str
        The string with non-latin letters removed.

    Examples
    --------
    >>> alphanumeric("What? À special word!")
    'What  special word'
    """
    return "".join(
        c
        for c in string
        if c in "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
    )

ignore_case(string: str, lower: bool = True) -> str #

Removes case from a string.

Parameters:

Name Type Description Default
string str

The string to remove case from.

required
lower bool

If to convert the string to all lower case, by default True. If set to False, converts it to all upper case.

True

Returns:

Type Description
str

The string with case removed.

Examples:

>>> ignore_case("Hello there!")
'hello there!'
>>> ignore_case("Hello there!", lower=False)
'HELLO THERE!'
Source code in stringmatch/strings.py
def ignore_case(self, string: str, lower: bool = True) -> str:
    """Removes case from a string.

    Parameters
    ----------
    string : str
        The string to remove case from.
    lower : bool, optional
        If to convert the string to all lower case, by default True.
        If set to False, converts it to all upper case.

    Returns
    -------
    str
        The string with case removed.

    Examples
    --------
    >>> ignore_case("Hello there!")
    'hello there!'
    >>> ignore_case("Hello there!", lower=False)
    'HELLO THERE!'
    """
    return string.lower() if lower else string.upper()