title: How to convert *any* Python object into a string?
body: <p>I want to concatenate a list of various Python objects into one string. The objects can be literally anything. I thought I could simply do this using the following code:</p>
  
  <pre><code>' '.join([str(x) for x in the_list])
  </code></pre>
  
  <p>but unfortunately that sometimes gives me a UnicodeEncodeError:</p>
  
  <pre><code>UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 80: ordinal not in range(128)
  </code></pre>
  
  <p>in <a href="https://stackoverflow.com/a/9942822/1650012">this SO answer</a> I found someone who says that I need to use <code>.encode('utf-8')</code>, so I changed my code to this:</p>
  
  <pre><code>' '.join([x.encode('utf-8') for x in the_list])
  </code></pre>
  
  <p>But if the objects are not strings or unicodes but for example <code>int</code>s I get an <code>AttributeError: 'int' object has no attribute 'encode'</code>. So this means I need to use some kind of if-statement to check what kind of type it is and how to convert it. But when should I use <code>.encode('utf-8')</code> and when should I use <code>str()</code>? </p>
  
  <p>It would be even better if I could also do some kind of oneliner for this, but I wouldn't know how? Does anybody else know? All tips are welcome!</p>