Python’s data types are one of the most important parts of the programming language. You can almost think of them as analogous to the nouns in a spoken language. But just as you can’t pair every verb with every noun in English, you can’t pair every data type with a function, operation, or operator. If you’re encountering a “typeerror: not all arguments converted during string formatting” error then you’re getting first-hand proof of that fact. But you’ll soon see why the error appears and how to fix it.
Data Types and Type Errors
The error message is conveying a lot of information and it’s not always immediately clear what it actually means. But the most important part of the error message is the declaration of a typeerror. A typeerror occurs when there’s a mismatch between data types. We can garner more information by looking at the text which follows the typeerror declaration.
The error message points out that the typeerror arose from a string formatting error. This can still cover a wide variety of different operations. But in general, this narrows things down. A string formatting typeerror usually refers to an attempt to work with strings causing a conflict between incompatible data types. For example, trying to run a mathematical operation on the letter “A”.
A Deeper Dive Into Type Errors
A Python typeerror shows that one of Python’s greatest strengths, flexibility, can also have some downsides. There’s a wide variety of ways to perform any given task. And the fact that Python’s variables aren’t as tightly controlled as in many other programming languages can cause some problems. Consider the following Python code to see how this issue can appear in your code.
ourNumber = “10”
ourSecondNumber = 5
result = ourNumber % ourSecondNumber
print(“Our result is” + str(result) + ” using ” + str(ourNumber) + ” and ” + str(ourSecondNumber))
This Python script is fairly straightforward. We begin by creating two numbers, ourNumber and ourSecondNumber. We then divide ourNumber by ourSecondNumber. Next, we print out the result in a nicely formatted string by using the str method to convert integers into string format. Or, rather, that’s what seems to be happening at first glance. In reality, there are a number of pitfalls here. And they all contribute to the “typeerror: not all arguments converted during string formatting” problem. As you might have seen already, running this script causes the Python interpreter to exit out with the “typeerror: not all arguments converted during string formatting” error message.
The first problem is that ourNumber isn’t an integer. We declared ourNumber as a string in this example. In real-world situations, this type of issue usually occurs when a Python script gets a number from a stream of some kind. Whether from a keyboard or text file. But no matter what the underlying reason, having something we think is an integer actually be in string format is a big problem. Especially since we proceed to declare ourSecondNumber as a true integer.
If we were using two integers then Python would divide ourNumber by ourSecondNumber. But since ourNumber is a string, Python doesn’t know what it should do. This causes the typeerror since a string can’t be divided by an integer. The error message might suggest to you that the problem came from incorrect string manipulation in the last line. But in reality, the interpreter never even reached the stage where it would format and print strings to screen. Consider the following code to see another example of how this problem can arise.
ourString = “saying”
ourSecondString = 1
print(“{0} hello to the {1} world”% ourString, ourSecondString)
In this example, we use multiple arguments within a print statement. We receive the same typeerror because we’re attempting to perform an unsupported replacement on a traditional string. This same Python error will occur when we try a wide variety of different but ultimately incompatible operations on a string.
Fixing the TypeError
The somewhat complex description might make you think that you’ll need an equally complex solution. But fixing the issue is a relatively simple process. Take a look at the following code.
ourNumber = “10”
ourSecondNumber = 5
result = int(ourNumber) % int(ourSecondNumber)
print(“Our result is” + str(result) + ” using ” + str(ourNumber) + ” and ” + str(ourSecondNumber))
In this example, we simply use int to convert variables to integers. The order of operations ensures that both values are converted before being passed to the modulo operation. If this was a recurring problem we might want to wrap everything up into a function that would automatically clean any data passed into it. But what about the other example we looked at? Take a look at the following code.
ourString = “saying”
ourSecondString = 1
print(“%s hello to the %s world” % (ourString, ourSecondString))
The solution here comes from explicitly declaring that we’re using a string and an integer or decimal value through printf style format string notation. The Python documentation provides a wealth of options to use with this type of formatting. And this does take care of the earlier problem with the use of a modulo. However, we can still refine this a little more to reduce any further chance of a typeerror. Change line three to the following.
print(“{0} hello to the {1} world”.format(ourString, ourSecondString))
Note that the script runs cleanly with no errors, despite the fact that there’s no explicit declaration of variable types within the print statement. This procedure is now considered the correct way to implement string formatting within a Python script. This new method vastly simplifies how a script will format string values and takes care of some underlying type management.
In this example we use the curly braces to signal an insertion point. Essentially signaling that it’s a string placeholder value. Another interesting aspect of this system is that it also reduces the need for an explicit format specifier when using the format method. And there’s no need to worry about string length.
It’s important to keep in mind that the underlying mechanism is complex. But the examples here highlight that fixing this type error is fairly straightforward. It’s simply a matter of using the language’s built-in tools to ensure variables are compatible with each other.