How can I pass a tuple with a single string as an argument (e.g. to mulitprocessing.Process)? [duplicate]

This is a common gotcha in Python – if you want to have a tuple with only one element, you need to specify that it’s actually a tuple (and not just something with brackets around it) – this is done by adding a comma after the element.

To fix this, just put a comma after the string, inside the brackets:

p = multiprocessing.Process(target=write, args=('hello',))

That way, Python will recognise it as a tuple with a single element, as intended. Currently, Python is interpreting your code as just a string. However, it’s failing in this particular way because a string is effectively list of characters. So Python is thinking that you want to pass (‘h’, ‘e’, ‘l’, ‘l’, ‘o’). That’s why it’s saying “you gave me 5 parameters”.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)