TypeError: write() argument must be str, not tuple (Python)

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

TypeError: write() argument must be str, not tuple (Python) #

The Python "TypeError: write() argument must be str, not tuple" occurs when we try to write a tuple object to a file using the write() method. To solve the error, use the join() method to join the tuple into a string, e.g. my_file.write(','.join(my_tuple)).

typeerror write argument must be str not tuple

Here is an example of how the error occurs.

main.py
with open('example.txt', 'w', encoding='utf-8') as my_file: my_tuple = ('Alice', 'Bob', 'Carl') # ⛔️ TypeError: write() argument must be str, not dict my_file.write(my_tuple)
We passed a tuple object to the write() method, but the method can only be called with a string argument.

One way to solve the error is to use the str.join() method to join the tuple into a string.

main.py
with open('example.txt', 'w', encoding='utf-8') as my_file: my_tuple = ('Alice', 'Bob', 'Carl') my_file.write(','.join(my_tuple)) # 👉️ Alice,Bob,Carl

We used a comma as the separator between the items of the tuple, but you can use any other separator or an empty string if you don't need a separator between the elements.

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

Note that the method raises a TypeError if there are any non-string values in the iterable.

If your tuple contains numbers, or other types, convert all of the values to string before calling join().

main.py
my_tuple = ('a', 'b', 1, 2) all_strings = tuple(map(str, my_tuple)) print(all_strings) # 👉️ ('a', 'b', '1', '2') result = ''.join(all_strings) print(result) # 👉️ 'ab12'

The string the method is called on is used as the separator between elements.

main.py
my_tuple = ('a', 'b', 'c') result = '-'.join(my_tuple) print(result) # 👉️ 'a-b-c'

If you don't need a separator and just want to join the iterable's elements into a string, call the join() method on an empty string.

main.py
my_tuple = ('a', 'b', 'c') result = ''.join(my_tuple) print(result) # 👉️ 'abc'

Alternatively, you can pass the tuple to the str() class to convert it to a string before writing it to the file.

main.py
with open('example.txt', 'w', encoding='utf-8') as my_file: my_tuple = ('Alice', 'Bob', 'Carl') my_file.write(str(my_tuple)) # 👉️ ('Alice', 'Bob', 'Carl')

You can also convert the tuple to a JSON string before writing it to the file.

main.py
import json with open('example.txt', 'w', encoding='utf-8') as my_file: my_tuple = ('Alice', 'Bob', 'Carl') my_file.write(json.dumps(my_tuple)) # 👉️ ["Alice", "Bob", "Carl"]

The json.dumps method converts a Python object to a JSON formatted string.

Since JSON doesn't support tuples, the tuple gets converted to a list.

In case you declared a tuple by mistake, tuples are constructed in multiple ways:

  • Using a pair of parentheses () creates an empty tuple
  • Using a trailing comma - a, or (a,)
  • Separating items with commas - a, b or (a, b)
  • Using the tuple() constructor

If you aren't sure what type of object a variable stores, use the type() class.

main.py
my_tuple = 'hello', 'world' print(type(my_tuple)) # 👉️ <class 'tuple'> print(isinstance(my_tuple, tuple)) # 👉️ True my_str = 'hello world' print(type(my_str)) # 👉️ <class 'str'> print(isinstance(my_str, str)) # 👉️ True

The type class returns the type of an object.

The isinstance function returns True if the passed in object is an instance or a subclass of the passed in class.

Conclusion #

The Python "TypeError: write() argument must be str, not tuple" occurs when we try to write a tuple object to a file using the write() method. To solve the error, use the join() method to join the tuple into a string, e.g. my_file.write(','.join(my_tuple)).

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.