how to convert print output into variable/string with "more than 3 arguments"? (python 3)
<p>Basically I'm trying to take the print output for my code and make it into a variable.  I tried to do this by converting it to a string but it gave me an error saying "TypeError: str() takes at most 3 arguments (6 given)"</p>

<p>my code:</p>

<pre><code>x = [1,2,3,4,5]
print(*x, sep='_')  ##gives me the output "1_2_3_4_5"

#problem part:
a = str(*x, sep='_')
print(a)             ##gives error
</code></pre>

<p>Is there either a way to convert the output to a string despite the "6 argument" thing or some other "str()"-like thing that would work?</p>

<p>You can convert the list of ints to a list of strings using <a href="https://docs.python.org/3/library/functions.html#map" rel="nofollow"><code>map</code></a>, then use <a href="https://docs.python.org/3/library/stdtypes.html#str.join" rel="nofollow"><code>join</code></a> to bind them together:</p>

<pre><code>x = [1, 2, 3, 4, 5]
a = '_'.join(map(str, x))
print(a)  # '1_2_3_4_5'
</code></pre>

<p>Note: Your code did not work because <a href="https://docs.python.org/3/library/functions.html#print" rel="nofollow"><code>print</code></a> does some work for you:</p>

<blockquote>
  <p>All non-keyword arguments are converted to strings like str() does and written to the stream.</p>
</blockquote>

<p>So, print did the string conversion step for you. When you call <a href="https://docs.python.org/3/library/stdtypes.html#str" rel="nofollow"><code>str()</code></a>, it expects only an object to convert to a string, with two optional keyword arguments (<code>encoding</code> and <code>errors</code>). By calling <code>str(*x, sep='_')</code>, you are passing in 5 arguments plus a separator, which is not a valid call.</p>

python string python-3.x printing arguments