Sometimes we might face TypeError: not all arguments converted during string formatting error that occur when we have a syntax error or use incorrect syntax to format a string. The error clearly says that it is a TypeError which means we are doing something wrong with the data type. In this article, we will learn how we can solve TypeError: not all arguments converted during string formatting error and will understand what the error actually means. Moreover, we will also cover how we can solve errors in Python by taking TypeError: not all arguments converted during string formatting as an example.
TypeError: not all arguments converted during string formatting
There can be many possible reasons for getting TypeError: not all arguments converted during string formatting error while coding in Python. One reason could be when we tried to add strings but couldn’t be added because of TypeError.
Many people confuse Python with other programming languages and when they tried to add a string to another string, they use % symbol which raises this error. For example, see the Python script below:
string1 = 'Bashir'
print("My name is {0}" % string1)
Output:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_20606/3317768937.py in <module>
1 string1 = 'Bashir'
2
----> 3 print("My name is {0}" % string1)
TypeError: not all arguments converted during string formatting
As you can see we get the same error. Now let us see how we can solve this issue with various methods:
Solution-1: Using format() method
In Python when we had to add two strings together, we use the formate method and pass the string values there.
For example, see the following code:
string1 = 'Bashir'
print("My name is {0}".format(string1))
Output:
My name is Bashir
The format() method help to place the string into the specified place within {} brackets.
If you have multiple strings values to add, you can use the same format() method with multiple parameters separated by commas:
string1 = 'Bashir'
string2 = "Alam"
print("My name is {0} {1}".format(string1, string2))
Output:
My name is Bashir Alam
Solution-2: Use the proper modulo operator on the Data type
We also get TypeError: not all arguments converted during string formatting when we tried to use the modulo operation on string types. For example, see the Python code below:
# defining string
string1 = '187'
# taking modulo
result = string1 % 3
Output:

If you are getting this error because of the modulo operation, then most probably you are trying to perform the modulo operation on the string value. In such case, you can change the string data type to integer and perform the operation:
# defining integer
string1 = 187
# taking modulo
result = string1 % 3
Now the code will execute without any error.
Solution-3: Using formatted string literal
An alternative solution to this problem is to use the formatted string literal. For example, see the code below:
string1 = 'Bashir'
string2 = "Alam"
print(f"My name is {string1} {string2}")
Output:
My name is Bashir Alam
As shown, we didn’t get any errors:
Solution-4: Using reference variable
Sometimes, this error can occur because we forget to reference the variable. In Python we can use %s for reference as shown below:
# defining name
string1 = "Bashir"
# %s referencing
result = "My name is: %s" %string1
The code will be executed without any error:
Understanding the error:
If the above-mentioned methods couldn’t help you to figure out the problem or you want to understand what the problem actually means, then you can continue reading this section:
In Python, the error itself gives a lot of information. For example, TypeError: not all arguments converted during string formatting this error has two main parts:
The first part of the error shows the category of the error and in this case, the error is TypeError followed by a colon, and then the second part of the error gives more information about the error. For example, in this case, it says not all arguments converted during string formatting which means we are doing something wrong with strings.
What is TypeError in Python?
The Python TypeError is an exception that occurs when the data type of an object in an operation is inappropriate. This can happen when an operation is performed on an object of an incorrect type, or it is not supported for the object
How to fix TypeError in Python?
You typically fix a TypeError by casting your value(s) to the correct type. In general, to fix a TypeError, think carefully about the types of values in the line that Python highlighted for you. The types are not compatible. So decide which types you really want, then use casts.
What is a string in Python?
Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string.
What are arguments in Python?
The terms parameter and argument can be used for the same thing: information that is passed into a function. From a function’s perspective: A parameter is the variable listed inside the parentheses in the function definition. An argument is a value that is sent to the function when it is called.
Summary
In this short article, we learned how we can solve TypeError: not all arguments converted during string formatting errors using various methods. We also discuss the reason for getting this error and explained the meaning of TypeError: not all arguments converted during string formatting errors in Python.
Related issues
- Typeerror: ‘float’ object is not iterable
- Importerror: DLL load failed: the specified procedure could not be found
- TypeError: ‘builtin_function_or_method’ object is not subscriptable
- ModuleNotFoundError: No module named ‘bs4’
- Typeerror: string indices must be integers
- TypeError: can’t multiply sequence by non-int of type ‘numpy.float64’
- TypeError: only integer scalar arrays can be converted to a scalar index
- ‘numpy.ndarray’ object has no attribute ‘append’
- numpy.core.multiarray failed to import
- Attributeerror: module matplotlib has no attribute subplots