Import ObjectId:
from bson import ObjectId
From ObjectId to string:
oid = ObjectId()
oid_str = str(oid)
# oid_str is now '555fc7956cda204928c9dbab'
From string to ObjectId:
oid_str = '555fc7956cda204928c9dbab'
oid2 = ObjectId(oid_str)
print(repr(oid2))
# ObjectId('555fc7956cda204928c9dbab')