Make doctests work on Python 2 and 3

As discussed in #2033 it would be nice to make the doctests work in Python 2 and 3 (though not urgent).