TheAlgorithms/Python · error · ValueError
plaintext is empty
Error message
plaintext is empty
What it means
Raised by autokey encrypt() when the plaintext is a str but empty (''). An empty plaintext would leave the autokey stream (key + plaintext) with nothing to encrypt and produce a meaningless empty ciphertext, so the function rejects it explicitly.
Source
Thrown at ciphers/autokey.py:44
Traceback (most recent call last):
...
ValueError: plaintext is empty
>>> encrypt("coffee is good as python", "")
Traceback (most recent call last):
...
ValueError: key is empty
>>> encrypt(527.26, "TheAlgorithms")
Traceback (most recent call last):
...
TypeError: plaintext must be a string
"""
if not isinstance(plaintext, str):
raise TypeError("plaintext must be a string")
if not isinstance(key, str):
raise TypeError("key must be a string")
if not plaintext:
raise ValueError("plaintext is empty")
if not key:
raise ValueError("key is empty")
key += plaintext
plaintext = plaintext.lower()
key = key.lower()
plaintext_iterator = 0
key_iterator = 0
ciphertext = ""
while plaintext_iterator < len(plaintext):
if (
ord(plaintext[plaintext_iterator]) < 97
or ord(plaintext[plaintext_iterator]) > 122
):
ciphertext += plaintext[plaintext_iterator]
plaintext_iterator += 1
elif ord(key[key_iterator]) < 97 or ord(key[key_iterator]) > 122:
key_iterator += 1View on GitHub (pinned to f5988cc097)
Solutions
- Skip empty inputs upstream: if not plaintext: continue / return '' early in your loop.
- Treat empty text as a no-op in your API: return '' without calling encrypt.
- Validate combined conditions once: if not isinstance(p, str) or not p: raise ValueError(...).
Example fix
# before
encrypt('', 'TheAlgorithms') # ValueError: plaintext is empty
# after
if plaintext:
ciphertext = encrypt(plaintext, 'TheAlgorithms')
else:
ciphertext = '' Defensive patterns
Strategy: validation
Validate before calling
if not plaintext:
return '' # nothing to encrypt
# only now call encrypt(plaintext, key) Try / catch
try:
encrypt(p, k)
except ValueError as e:
if 'empty' in str(e):
ciphertext = ''
else:
raise Prevention
- Short-circuit empty strings in message loops before encryption.
- Filter blank lines/records at ingestion.
When it happens
Trigger: Calling encrypt('', 'TheAlgorithms'). The isinstance checks pass because '' is a str; only the emptiness triggers this.
Common situations: Empty form fields, blank lines read from a file, or filtered data that reduced to zero characters reaching the cipher.
Related errors
- plain must contain only lowercase letters (a-z)
- key is empty
- ciphertext is empty
- encode() accepts only letters of the alphabet and spaces
- decode() accepts only 'A', 'B' and spaces
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/dce8ef68b9c63eea.
Report an issue: GitHub.