What is the fool proof way to convert some string (utf-8 or else) to a simple ASCII string in python

ascii, decode, python, utf-8

Solution

If you want an ASCII string that unambiguously represents what you have got, without losing any information, the answer is simple:

Don't muck about with encode/decode, use the `repr()` function (Python 2.X) or the `ascii()` function (Python 3.x).

Problem

Inside my python scrip, I get some string back from a function which I didn't write. The encoding of it varies. I need to convert it to ascii format. Is there some fool-proof way of doing this? I don't mind replacing the non-ascii chars with blanks or something else...

Original source