If you literally want to raise an exception only on the empty string, you'll need to do that manually:

try:
    user_input = input() # raw_input in Python 2.x
    if not user_input:
        raise ValueError('empty string')
except ValueError as e:
    print(e)

But that "integer input" part of the comment makes me think what you really want is to raise an exception on anything other than an integer, including but not limited to the empty string.

If so, open up your interactive interpreter and see what happens when you type things like int('2'), int('abc'), int(''), etc., and the answer should be pretty obvious.

But then how do you distinguish an empty string from something different? Simple: Just do the user_input = input() before the try, and check whether user_input is empty within the except. (You put if statements inside except handlers all the time in real code, e.g., to distinguish an OSError with an EINTR errno from one with a different errno.)

Answer from abarnert on Stack Overflow
🌐
Python
docs.python.org › 3 › library › exceptions.html
Built-in Exceptions — Python 3.14.5 documentation
It is not meant to be directly inherited by user-defined classes (for that, use Exception). If str() is called on an instance of this class, the representation of the argument(s) to the instance are returned, or the empty string when there were no ...
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 381248 › using-try-except-to-catch-a-blank-input
python - Using Try, Except to catch a blank input [SOLVED] | DaniWeb
In python 3, after result = input(prompt) , result is a string in the python sense (an instance of the datatype 'str'). Examples of strings are · "" # the empty string " " # a string of white space "3.14159" # a string with the representation … — Gribouillis 1,391 Jump to Post · You can pass the acceptable values to the checkInput() function and let it do the work. For the getFloat() function, use a try/except to test for correct input.
🌐
w3resource
w3resource.com › python-exercises › extended-data-types › python-extended-data-types-none-exercise-1.php
Python function to handle empty strings
August 11, 2025 - Write a Python function that takes a string as input and returns "None" if the string is empty, otherwise it returns the given string. ... def check_string(input_string): if not input_string: return "None" return input_string def main(): try: ...
🌐
CopyProgramming
copyprogramming.com › howto › python-empty-list-exception
Python: Exception Raised When Using an Empty List in Python
April 9, 2023 - Empty tuple as argument to except in python, The answer is 1: no exceptions, in Python 2 and in Python 3. exceptions = () try: a = 1 / 0 except exceptions as e: print ("the answer is 2") Traceback (most recent call last): File "<pyshell#38>", line 2, in <module> a = 1 / 0 ZeroDivisionError: integer division or modulo by zero. If you want to the … ... In the try clause, I am assigning values to multiple variables . However, I need an except clause that sets an empty string only for the variables that are unavailable.
🌐
CodeQL
codeql.github.com › codeql-query-help › python › py-empty-except
Empty except — CodeQL query help documentation
An empty except block may be an indication that the programmer intended to handle the exception, but never wrote the code to do so. Ensure all exceptions are handled correctly. In this example, the program keeps running with the same privileges if it fails to drop to lower privileges.
Find elsewhere
🌐
Python
docs.python.org › 3.13 › library › exceptions.html
Built-in Exceptions — Python 3.13.3 documentation
It is not meant to be directly inherited by user-defined classes (for that, use Exception). If str() is called on an instance of this class, the representation of the argument(s) to the instance are returned, or the empty string when there were no ...
🌐
University of Toronto
teach.cs.toronto.edu › ~csc148h › winter › notes › abstract-data-types › exceptions.html
3.3 Exceptions
class EmptyStackError(Exception): """Exception raised when calling pop on an empty stack.""" def __str__(self) -> str: """Return a string representation of this error.""" return 'You called pop on an empty stack.
🌐
Python
docs.python.org › 3 › library › exceptions.html
Built-in Exceptions — Python 3.14.4 documentation
It is not meant to be directly inherited by user-defined classes (for that, use Exception). If str() is called on an instance of this class, the representation of the argument(s) to the instance are returned, or the empty string when there were no ...
🌐
Python
docs.python.org › 3 › c-api › exceptions.html
Exception Handling — Python 3.14.5 documentation
This function sets the error indicator and returns NULL. exception should be a Python exception class. The format and subsequent parameters help format the error message; they have the same meaning and values as in PyUnicode_FromFormat(). format is an ASCII-encoded string.
🌐
Python
docs.python.org › 3.15 › library › exceptions.html
Built-in Exceptions — Python 3.15.0a2 documentation
It is not meant to be directly inherited by user-defined classes (for that, use Exception). If str() is called on an instance of this class, the representation of the argument(s) to the instance are returned, or the empty string when there were no ...
🌐
Sentry
sentry.io › sentry answers › python › check if a string is empty in python
Check if a string is empty in Python | Sentry
May 15, 2023 - if my_string.strip() == "": print("my_string is an empty string!") else: print("my_string is not an empty string!") ... Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski. SEE EPISODES ... David Y. — July 15, 2023 · Difference between `@staticmethod` and `@classmethod` function decorators in Python
Top answer
1 of 3
4

The thing that I think you're missing here is that the StopIteration is what actually makes the for loop stop. This is why you can write custom iterators that work with Python's for loop transparently. It doesn't do any checking to see if the iterator is empty, and the for loop does not keep track of of the iterator's state. This is an effect of the iterator protocol, and was an intentional choice in the language design. You can read more about the iterator protocol on Python's docs site if you want to.

It also makes more sense if you think about the number of items in e.g. your list corresponding directly to the number of times your loop is executed.

-----------------------------------|
| No. of items | No. times executed|
------------------------------------
|      5       |         5         |
|      4       |         4         |
|      3       |         3         |
|      2       |         2         |
|      1       |         1         |
|      0       |         0         |
------------------------------------

If the for loop special-cased an empty iterable, this invariant would be lost.

It would also complicate the protocol on writing custom iterators, because you would have to have an extra method to signal to the for loop that your iterable is empty before the iteration began.

Here's a (stupid) example of a custom iterator that always acts like it's empty:

class EmptyIterator():
    def __iter__(self): return self
    def __next__(self): raise StopIteration

for blah in EmptyIterator():
    print('this is never reached')
try:
    next(EmptyIterator())
except StopIteration:
    print('Oh hi, I was empty so I raised this Exception for you.')
2 of 3
10

This is odd to me. Why should an empty collection be treated any differently?

Forcing the programmer to check if the collection is empty before doing things to it would be a widespread, problematic sort of burden. Worse, an empty collection is in no way exceptional.

Personally, I would rather have the occasional bug where a no-op happened because I forgot to check for the rare case where I wanted different behavior on an empty collection rather than the occasional bug where an exception crashes my app because I forgot to say if empty, do nothing everywhere I wanted that common, expected behavior.

I mean, do you think printing empty strings should throw exceptions?