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.4 documentation
The base class for all built-in exceptions. 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 ...
Discussions

.Empty string comprehension?
Hello everyone , I wrote this code that I understand except one detail in it , here’s the code : person_infos(first_name, last_name, age=''): person = {'first': first_name, 'last': last_name} if age: per… More on discuss.python.org
🌐 discuss.python.org
4
1
March 14, 2020
How to check if the string is empty in Python? - Stack Overflow
Does Python have something like an empty string variable where you can do: if myString == string.empty: Regardless, what's the most elegant way to check for empty string values? I find hard coding... More on stackoverflow.com
🌐 stackoverflow.com
Python not recognizing an empty string ('') as an empty string
Firstly, I would strongly suggest avoiding phrasing like "Python not recognizing '' as empty string", since that's highly unlikely and it's probably just a bug in your program or unexpected data type in your data. It's never a compiler error, as they say. Be willing to accept you have a bug. You've provided the output of your code, but can you provide the code itself? After all, in your output it clearly looks like empty strings, but that's your output and not necessarily a source of truth. That would be the code + sample data. My debug approach for this would be to add more print statements. Check the type() of the variable you're concerned with. Print its len(). And repr(). Litter the area surrounding your if-statement with prints to understand exactly every choice that Python is making. Sometimes, I'll take the condition from an if and say print(variable == '') so that I can see the False for myself. Especially in the case of and/or conditions so I can make sure each piece is correct. One last note, [] is a list, not a dict. Dicts are {key: value}. More on reddit.com
🌐 r/learnpython
5
0
February 22, 2020
exception - MemoryError's message as str is empty in Python - Stack Overflow
It is not meant to be directly inherited by user-defined classes (for that, use Exception). If str() or unicode() 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 arguments. More on stackoverflow.com
🌐 stackoverflow.com
🌐
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 - In Python, empty strings are considered equivalent to False for boolean operations.
🌐
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.
🌐
Python
bugs.python.org › issue24243
Issue 24243: behavior for finding an empty string is inconsistent with documentation - Python tracker
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/68431
🌐
GeeksforGeeks
geeksforgeeks.org › python-program-to-check-if-string-is-empty-or-not
Check if String is Empty or Not - Python - GeeksforGeeks
April 12, 2025 - The string is empty. Explanation: "not s" negates the boolean value of s, if it's empty then it's boolean quivalent to False and its negation would be True. ... Sometimes, while working with Python, we can have a problem in which we need to ...
🌐
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: ...
Find elsewhere
🌐
Codecademy
codecademy.com › forum_questions › 52837bfe80ff33cc480027d3
when I leave the input empty it gives me an error string index out of range | Codecademy
when I leave the input for enter word empty it should print empty but instead it says · “Traceback (most recent call last): File “python”, line 6, in
🌐
Quora
quora.com › Does-Python-have-something-like-an-empty-string-variable-where-you-can-do-if-myString-string-empty-Regardless-whats-the-most-elegant-way-to-check-for-empty-string-values
Does Python have something like an empty string variable where you can do if myString == string.empty? Regardless, what's the most elegant way to check for empty string values? - Quora
except NameError: print ("val wasn\'t set, or is not in scope") Names have no type, the value has the type · Values have no scope, the name does. Only names can be deleted. Values are kept or destroyed by the garbage collector when they have no references to them. Names are Python’s variables, but they are really just references to values. Upvote · · 9911 · 92 · 91 · RelatedHow do I check if the string is empty in Python?
🌐
Python.org
discuss.python.org › python help
.Empty string comprehension? - Python Help - Discussions on Python.org
March 14, 2020 - Hello everyone , I wrote this code ... return person a = person_infos('fna', 'lna', age=34) print(a) The variable age in the function definition is an empty string , but we when set age as a number ( age = 4 ) no error happens , ...
🌐
Reddit
reddit.com › r/learnpython › python not recognizing an empty string ('') as an empty string
r/learnpython on Reddit: Python not recognizing an empty string ('') as an empty string
February 22, 2020 -

How come Python is recognizing an empty string ('') (two single quotes not a double quote) as something other than that? type() returns <type 'str'>, repr() returns repr: '', len() returns len: 0

Source python file: https://pastebin.com/kDxYSpwJ

the three source data files:

  1. https://pastebin.com/TyVh0Njd

  2. https://pastebin.com/2gGBDdP4

  3. https://pastebin.com/02yTymJn

The issue in the code starts in the for loop on line 281.

The below is the output of a section of code that I'm running into problems with. I have a dictionary of products (in this case, product '00835K') and each product has multiple items, seven of which are potential components that make up that product (A1 - A7)

I'm using the comp dictionary to iterate through the different components of all the products, ex: 'comp_prod_%s' % comp[comp_index], printing out the keys and values of each one, then checking if each one is an empty string or not. If not, append that value to a dict called dest_prod.

In the section containing the seven component keys (ac2_products['00835K']['comp_prod_A1]...) and the component A4 is listed as an empty string '' but when the check comes to seeing if that value is not equal to '', it fails assuming that '' != ''

Why would Python be assuming/treating this value, that is clearly an empty string as if it weren't?

comp = ['A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7']
00835K
	ac2_products['00835K']['comp_prod_A1'] = '000804'
	ac2_products['00835K']['comp_prod_A2'] = '000808'
	ac2_products['00835K']['comp_prod_A3'] = '000850'
	ac2_products['00835K']['comp_prod_A4'] = ''
	ac2_products['00835K']['comp_prod_A5'] = ''
	ac2_products['00835K']['comp_prod_A6'] = ''
	ac2_products['00835K']['comp_prod_A7'] = ''

ac2_products['00835K']['comp_prod_A1'], comp_index: 0
ac2_products['00835K']['comp_prod_A1'] != '' (000804)
1: Appended 000804 to 'dest_prod' and 08300000 'dest_pct'

ac2_products['00835K']['comp_prod_A2'], comp_index: 1
ac2_products['00835K']['comp_prod_A2'] != '' (000808)
1: Appended 000808 to 'dest_prod' and 01100000 'dest_pct'

ac2_products['00835K']['comp_prod_A3'], comp_index: 2
ac2_products['00835K']['comp_prod_A3'] != '' (000850)
1: Appended 000850 to 'dest_prod' and 00500000 'dest_pct'

ac2_products['00835K']['comp_prod_A4'], comp_index: 3
ac2_products['00835K']['comp_prod_A4'] != '' ()
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python string tutorial › python test empty string
Python Test Empty String | Examples of Python Test Empty String
April 15, 2023 - We understood how to check the strings which has zero value and how they are not able to detect strings which have just spaces through multiple examples and methods. We hope that this EDUCBA information on “Python Test Empty String” was beneficial to you.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
LabEx
labex.io › tutorials › python-how-to-manage-empty-string-inputs-438180
How to manage empty string inputs | LabEx
def read_file_safely(filename, default_content=""): try: with open(filename, 'r') as file: content = file.read().strip() return content if content else default_content except FileNotFoundError: return default_content ... By implementing these strategies, developers can create more resilient and user-friendly applications that gracefully manage string inputs. By mastering empty string input management in Python, developers can create more resilient and user-friendly applications.
🌐
Enterprise DNA
blog.enterprisedna.co › python-empty-string
Python Empty String: Explained With Examples – Master Data Skills + AI
You can compare a string variable with a quoted empty string using the eq operator to check for equality. This code snippet illustrates how: ... The len() function returns the length of Python strings.
🌐
PythonHow
pythonhow.com › how › check-if-a-string-is-empty
Here is how to check if a string is empty in Python
my_string = "" if not bool(my_string): print("String is empty.") ... Solve Python exercises and get instant AI feedback on your solutions.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › check if string is empty or not in python
Check if String is Empty or Not in Python - Spark By {Examples}
May 21, 2024 - How to check if the Python String is empty or not? In python, there are several ways to check if the string is empty or not. Empty strings are considered
🌐
Syntx Scenarios
syntaxscenarios.com › home › python › how to check if a string is empty in python?
How to Check if a String is Empty in Python?
September 24, 2025 - A try-except block can handle unexpected catches and errors. It can also be used to check if a string is empty in Python, ensuring your program doesn’t crash when something goes wrong.