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><code>*x</code> supplies much too many arguments to <code>str()</code> causing the error, and <code>sep</code> isn't an argument of <code>str()</code>. <code>str()</code> expects one object to be converted, and <code>*x</code> gives 5. Try this:</p>

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

<p>This will go through the list and join each element together with an underscore between.</p>

<p><a href="https://ideone.com/dLHFqx" rel="nofollow">Try it on IDEOne</a></p>

<hr>

<p>You can also use <code>map(function, sequence)</code> to shorten the <code>join()</code>. The function being applied is <code>str()</code> to convert the numbers into string form, and to the <code>x</code> list. Here's with <code>map()</code>:</p>

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

python string python-3.x printing arguments