What is the best (idiomatic) way to check the type of a Python variable?

I need to know if a variable in Python is a string or a dict. Is there anything wrong with the following code? if type(x) == type(str()): do_something_with_a_string(x) elif type(x) == type(dict()): do_somethting_with_a_dict(x) else: raise ValueError Update: I accepted avisser’s answer (though I will change my mind if someone explains why isinstance is preferred over … Read more