The hasattr() function in Python is used to check if an object has a specified attribute. It returns True if the attribute exists, and False if it does not. This function is particularly useful for verifying the presence of an attribute in an object before attempting to access or modify it, which can help avoid runtime errors.