Sometimes we might face TypeError not all arguments converted during string formatting error that occurs 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.
Solve TypeError not all arguments converted during string formatting in Python
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 helps 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.
Reasons for getting TypeError not all arguments converted during string formatting error
This error occurs when you try to format a string using the str.format()
method or the %
operator, but you pass too few or too many arguments than required by the formatting string. Here are some common reasons why you might get this error:
- An incorrect number of arguments: You might have specified too few or too many arguments in your format string. For example, if your format string has two placeholders but you only provide one argument, you will get this error.
- Incorrect format codes: You might have used an incorrect format code in your format string. For example, if you use
%d
format code to format a string, you will get this error. - Incorrect argument type: You might be trying to format an argument that is of the wrong type. For example, if you try to format a string using
%d
format code, but the argument is a string, you will get this error. - Missing or misplaced curly braces: If you’re using the
str.format()
method, you may have misplaced or forgotten a curly brace. For example, if you have a string like"Hello, {}!"
and you forget to put a value inside the curly braces, you will get this error.
To resolve this error, make sure that your format string has the correct number of placeholders, that you’re using the correct format codes, that your arguments have the correct type, and that your curly braces are correctly placed.
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 Errors
- ModuleNotFoundError: No module named ‘cv2’ in Python Solved
- [5 methods] Zipfile.badzipfile: file is not a zip file
- [Solved] AttributeError: ‘list’ object has no attribute ‘split’
- ImportError: numba needs numpy 1.21 or less-Solved
- TypeError: only size-1 arrays can be converted to Python scalars solved