The story involving Bhima, Python and Yudhishtira is mentioned in the Vana Parva of the Mahabharata. The story has it that a python caught Bhima in its coils; despite his immense strength, Bhima was unable to free himself. Yudhishtira then comes in search of Bhima and is puzzled how the strong Bhima is in the coil of a python.
The python then explains to Yudhishtira that Bhima will only escape if someone answers his questions correctly.
The question and answer session begins and Yudhishtira correctly answers all questions. Yudhishtira who was amazed by the knowledge of python and himself asks it several questions.
When the question and answer session was over, the python releases Bhima and suddenly turns into a human being. The man then narrates his story. His was King Nahusha and was from the same lineage of the Pandavas. He was cursed by Sage Agastya due to his pride and arrogance. The sage had then predicted that he will get back the human form when a man answers correctly all his questions. He thus caught several men but no one gave him satisfactory answers. He then killed them and swallowed them.
Yudhisthira by answering the questions rids him of the curse. Nahusha then blesses Yudhisthira and Bhima.