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)"
x = [1,2,3,4,5]
print(*x, sep='_') ##gives me the output "1_2_3_4_5"
a = str(*x, sep='_')
print(a) ##gives error
x = [1, 2, 3, 4, 5] a = '_'.join(map(str, x)) print(a) # '1_2_3_4_5'
Note: Your code did not work because
All non-keyword arguments are converted to strings like str() does and written to the stream.
So, print did the string conversion step for you. When you call
str(), it expects only an object to convert to a string, with two optional keyword arguments (
errors). By calling
str(*x, sep='_'), you are passing in 5 arguments plus a separator, which is not a valid call.