Skip to content

ItIs

Tools for determining something.

The module contains the following functions:

  • is_number - Check if a string is a number.
  • is_palindrome - Check if a string is a palindrome.

is_number(value)

Check if a string is a number.

Only decimal numbers.

Examples:

>>> from xloft import is_number
>>> is_number("123")
True

Parameters:

Name Type Description Default
value str

Some kind of string.

required

Returns:

Type Description
bool

True, if the string is a number.

Source code in src/xloft/itis.py
def is_number(value: str) -> bool:
    """Check if a string is a number.

    Only decimal numbers.

    Examples:
        >>> from xloft import is_number
        >>> is_number("123")
        True

    Args:
        value (str): Some kind of string.

    Returns:
        True, if the string is a number.
    """
    try:
        float(value)
        return True
    except ValueError:
        return False

is_palindrome(value)

Check if a string is a palindrome.

Examples:

>>> from xloft import is_palindrome
>>> is_palindrome("Go hang a salami, I'm a lasagna hog")
True

Parameters:

Name Type Description Default
value str

Alpha-numeric string.

required

Returns:

Type Description
bool

Boolean value.

Source code in src/xloft/itis.py
def is_palindrome(value: str) -> bool:
    """Check if a string is a palindrome.

    Examples:
        >>> from xloft import is_palindrome
        >>> is_palindrome("Go hang a salami, I'm a lasagna hog")
        True

    Args:
        value (str): Alpha-numeric string.

    Returns:
        Boolean value.
    """
    if not isinstance(value, str):
        raise TypeError("The value is not a string!")
    if not len(value):
        raise ValueError("The string must not be empty!")
    string_list = [char.lower() for char in value if char.isalnum()]
    reverse_list = string_list[::-1]
    return reverse_list == string_list