可以使用以下代碼來檢查字典中的特定鍵是否為“”:
my_dict = {'key1': 'value1', 'key2': '', 'key3': 'value3'}
if my_dict['key2'] == "":
print("Key2 is an empty string.")
else:
print("Key2 is not an empty string.")
如果需要更嚴格的檢查,可以使用以下代碼:
if 'key2' in my_dict and my_dict['key2'] == "":
print("Key2 is an empty string.")
else:
print("Key2 is not an empty string or does not exist.")
上述代碼首先使用in運算符檢查鍵是否在字典中,然后再檢查該鍵的值是否為“”。