{"record":{"id":"44ebf2fc909babaa","repo":"TheAlgorithms/Python","slug":"plaintext-must-be-a-string","errorCode":null,"errorMessage":"plaintext must be a string","messagePattern":"plaintext must be a string","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ciphers/autokey.py","lineNumber":39,"sourceCode":"    >>> encrypt(\"coffee is good as python\", 2)\n    Traceback (most recent call last):\n        ...\n    TypeError: key must be a string\n    >>> encrypt(\"\", \"TheAlgorithms\")\n    Traceback (most recent call last):\n        ...\n    ValueError: plaintext is empty\n    >>> encrypt(\"coffee is good as python\", \"\")\n    Traceback (most recent call last):\n        ...\n    ValueError: key is empty\n    >>> encrypt(527.26, \"TheAlgorithms\")\n    Traceback (most recent call last):\n        ...\n    TypeError: plaintext must be a string\n    \"\"\"\n    if not isinstance(plaintext, str):\n        raise TypeError(\"plaintext must be a string\")\n    if not isinstance(key, str):\n        raise TypeError(\"key must be a string\")\n\n    if not plaintext:\n        raise ValueError(\"plaintext is empty\")\n    if not key:\n        raise ValueError(\"key is empty\")\n\n    key += plaintext\n    plaintext = plaintext.lower()\n    key = key.lower()\n    plaintext_iterator = 0\n    key_iterator = 0\n    ciphertext = \"\"\n    while plaintext_iterator < len(plaintext):\n        if (\n            ord(plaintext[plaintext_iterator]) < 97\n            or ord(plaintext[plaintext_iterator]) > 122","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/ciphers/autokey.py#L21-L57","documentation":"Raised by autokey encrypt() when the plaintext argument is not a str. The cipher works character-by-character with ord()/string indexing on both plaintext and key, so non-string input is rejected by an isinstance check before any processing.","triggerScenarios":"Calling encrypt(527.26, 'TheAlgorithms'), encrypt(None, 'key'), encrypt(['a'], 'key'), or any non-str first argument.","commonSituations":"Passing numbers or bytes from upstream data pipelines, or calling encrypt/decrypt with swapped argument order (ciphertext/key confusion).","solutions":["Pass a str: encrypt(str(plaintext), key) if the value is a number or other printable object.","Decode bytes first: encrypt(data.decode('utf-8'), key).","Check argument order — plaintext is the first parameter for encrypt()."],"exampleFix":"# before\nencrypt(527.26, 'TheAlgorithms')  # TypeError: plaintext must be a string\n\n# after\nencrypt(str(527.26), 'TheAlgorithms')","handlingStrategy":"type-guard","validationCode":"if not isinstance(plaintext, str):\n    plaintext = str(plaintext)","typeGuard":"def is_str(v: object) -> bool:\n    return isinstance(v, str)","tryCatchPattern":"try:\n    encrypt(p, k)\nexcept TypeError:\n    p = str(p)\n    ciphertext = encrypt(p, k)","preventionTips":["Keep argument order documented: encrypt(plaintext, key).","Decode bytes to str at the system boundary."],"tags":["cipher","type-error","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}