The reason and solution of the error of join function: sequence item 0: expected STR instance, int found

When I was writing code the other day, the task was to separate the incoming parameters with “_” and then append them to the file
Direct up code

def operate(file_path, *args):
    f = open(file_path, mode="a", encoding="utf-8")
    s = ""
    s = '_'.join(args)
    f.write(s)
    f.flush()
    f.close()
operate("hhhh.txt", 123, 456, 789)

Receive a string, but I wrote a number by mistake, then reported an error
TypeError: sequence item 0: expected STR instance, int found TypeError: sequence item 0: expected STR instance, int found TypeError: sequence item 0: expected STR instance, int found
Then look for the document, and here’s the original text
Return a string which is the concatenation of the strings in iterable. A TypeError will be raised if there are any non-string values in iterable, including bytes objects. The separator between elements is the string providing this method.
The join function is a string function, and the arguments and inserts are strings
So:
S = ‘_’.join(args) to

s = '_'.join(str(args).strip())

That’s it!
 
 
 

Read More: