{"record":{"id":"45371249a4ebb54a","repo":"TheAlgorithms/Python","slug":"empty-string-was-passed-to-the-function-453712","errorCode":null,"errorMessage":"Empty string was passed to the function","messagePattern":"Empty string was passed to the function","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"conversions/binary_to_hexadecimal.py","lineNumber":45,"sourceCode":"    >>> bin_to_hexadecimal(' 1010   ')\n    '0x0a'\n    >>> bin_to_hexadecimal('-11101')\n    '-0x1d'\n    >>> bin_to_hexadecimal('a')\n    Traceback (most recent call last):\n        ...\n    ValueError: Non-binary value was passed to the function\n    >>> bin_to_hexadecimal('')\n    Traceback (most recent call last):\n        ...\n    ValueError: Empty string was passed to the function\n    \"\"\"\n    # Sanitising parameter\n    binary_str = str(binary_str).strip()\n\n    # Exceptions\n    if not binary_str:\n        raise ValueError(\"Empty string was passed to the function\")\n    is_negative = binary_str[0] == \"-\"\n    binary_str = binary_str[1:] if is_negative else binary_str\n    if not all(char in \"01\" for char in binary_str):\n        raise ValueError(\"Non-binary value was passed to the function\")\n\n    binary_str = (\n        \"0\" * (4 * (divmod(len(binary_str), 4)[0] + 1) - len(binary_str)) + binary_str\n    )\n\n    hexadecimal = []\n    for x in range(0, len(binary_str), 4):\n        hexadecimal.append(BITS_TO_HEX[binary_str[x : x + 4]])\n    hexadecimal_str = \"0x\" + \"\".join(hexadecimal)\n\n    return \"-\" + hexadecimal_str if is_negative else hexadecimal_str\n\n\nif __name__ == \"__main__\":","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/binary_to_hexadecimal.py#L27-L63","documentation":"bin_to_hexadecimal raises this ValueError when the input string is empty after str() conversion and .strip(). The empty check runs before sign handling and digit validation, so blank input is reported specifically as 'empty' rather than 'non-binary', which makes caller-side error messages actionable.","triggerScenarios":"bin_to_hexadecimal(''), bin_to_hexadecimal('   '), bin_to_hexadecimal('\\t') — any whitespace-only or empty value.","commonSituations":"Form fields submitted empty; split() producing empty tokens when the delimiter repeats ('101,,111'.split(',')); CSV columns with missing values passed through unchanged.","solutions":["Guard before calling: if not binary_str.strip(): handle missing value","Skip empty tokens when mapping over collections: filter(None, map(str.strip, tokens))","Treat empty as '0' if that is the intended domain behavior: binary_str or '0'"],"exampleFix":"# before\nvalues = '101,,111'.split(',')\n[bin_to_hexadecimal(v) for v in values]\n# ValueError: Empty string was passed to the function\n\n# after\n[bin_to_hexadecimal(v) for v in values if v.strip()]","handlingStrategy":"validation","validationCode":"binary_str = str(binary_str or '').strip()\nif not binary_str:\n    binary_str = '0'\nbin_to_hexadecimal(binary_str)","typeGuard":"def is_nonempty_str(v) -> bool:\n    return isinstance(v, str) and v.strip() != ''","tryCatchPattern":"try:\n    bin_to_hexadecimal(s)\nexcept ValueError as e:\n    if 'Empty string' in str(e):\n        return '0x0'\n    raise","preventionTips":["filter(None, map(str.strip, tokens)) before mapping conversions","Treat missing CSV fields as explicit defaults","Require non-empty input in form validation"],"tags":["conversions","binary","hexadecimal","empty-input"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}