# Not all arguments converted during string formatting in Python – How To Solve It?

“Not all arguments converted during string formatting in Python” is a standard error related to data type in the string formatting function. The below explanations can help you know more about the cause of this error and solutions.

## How does the error “Not all arguments converted during string formatting” in Python happen?

A TypeError is a specific error that informs us that a value of a specific type cannot be used for the task we are conducting. It happens when an operation’s object data types are incorrect. Our data type issue here is a string value.

Python supports a wide range of formatting techniques for strings. This enables you to append values to all strings’ ends or insert values within strings.

There are two popular methods for formatting strings:

• Employing the % operator
• Combining the .format() method with the operator

The error will occur if you try to combine the two syntaxes or mismatch data types and strings are not correctly formatted. For example:

Example 1:

# Get the number from the user
num = input("Enter your number: ")

# Get the surplus of your number when divided by 7
surplus = num % 7
if surplus == 0:
print("Your number is divisible by 7")
else:
print("Your number isn't divisible by 7")

In this program, the variable number entered by the user is a string, so the modulus operator % can’t be executed and cannot be divided by an integer 7. Therefore, “TypeError: Not all arguments converted during string formatting” appeared.

Here is another example if you try to combine the two syntaxes.

Example 2:

# Input Product and cost from user
product = input("Enter product: ")
buyPrice = input("Enter buying price($): ") sellPrice = input("Enter selling price($): ")

# Print profit of the product
print("The profit of {} is {}$. " % product, str(profit)) In this example, “profit” has been transformed into a string and combined with our string. ## How to solve this error? ### Converting variable To perform mathematical operations in python, the first thing to do is to convert the variables to numeric data types such as int, float, and complex. Now let’s fix example 1: Example 1: The error occurs because we are trying to divide a string by an integer. So the simple fix is to type conversion string to an integer. # Get the number from the user and convert it to an integer num = int(input("Enter your number: ")) # Get the surplus of your number when divided by 7 surplus = num % 7 if surplus == 0: print("Your number is divisible by 7") else: print("Your number isn't divisible by 7") Output: Enter your number: 9 Your number isn't divisible by 7 When running the program, you need to enter any number, and your input data will be a string and converted to an integer. Then the program will display the result of whether your number is divisible by 7 or not. ### Using .format() method Another way to overcome this problem is to call the .format() method on the string and enter values for each placeholder. Try this with example 2: # Input Product and cost from user product = input("Enter product: ") buyPrice = input("Enter buying price($): ")
sellPrice = input("Enter selling price($): ") profit = float(sellPrice) - float(buyPrice) # Print profit of the product print("The profit of {} is {}$. ".format(product, str(profit)))

Output:

Enter product: beer
Enter buying price($): 10 Enter selling price($): 12
The profit of beer is 2.0 $.  ### Using the old-style % string formatting technique Another way to fix the error is to call the % string formatting technique. Try this with example 2: # Input Product and cost from user product = input("Enter product : ") buyPrice = input("Enter buying price($) : ")
sellPrice = input("Enter selling price($) : ") profit = float(sellPrice) - float(buyPrice) # Print profit of the product print("The profit of %s is %s$. " % (product, str(profit)))

Output:

Enter product : rice
Enter buying price($) : 7 Enter selling price($) : 15
The profit of rice is 8.0 \$. 

## Summary

When Python doesn’t add all arguments to a string format operation, the “not all arguments converted during string formatting” in python error will occurs. This can occur if you try to combine the .format() method and employ the % operator or mismatch data types and strings are not correctly formatted. I hope you can fix your bug after this post. Good luck!

Maybe you are interested: