DEV Community

Srinivas for ItsMyCode

Posted on • Originally published at itsmycode.com on <time datetime="2021-09-04T04:52:04Z" class="date-no-year">Sep 4</time>

Python typeerror: not all arguments converted during string formatting

ItsMyCode |

In Python, typeerror: not all arguments converted during string formatting occurs mainly in 3 different cases.

  1. *Applying incorrect format Specifier *
  2. *Incorrect formatting and substitution of values during string interpolation *
  3. Mixing different types of format specifiers

Resolving typeerror: not all arguments converted during string formatting

In Python, *TypeError * occurs if you perform an operation or use a function on an object of a different type. Let us look at each of the scenarios in depth with examples and solutions to these issues.

Applying incorrect format Specifier

If you use the percentage symbol ( % ) on a string, it is used for formatting, and if you are using it on an integer, it is for calculating the modulo.

If you look at the below code to check odd or even numbers, we accept an input number in the form of string and perform modulus operation ( % ) on string variable. Since it cannot perform a division of string and get the reminder, Python will throw not all arguments converted during string formatting error.

# Check even or odd scenario
number= (input("Enter a Number: "))
if(number % 2):
    print("Given number is odd")
else:
    print("Given number is even")

# Output 
Enter a Number: 5
Traceback (most recent call last):
  File "c:\Projects\Tryouts\listindexerror.py", line 3, in <module>
    if(number % 2):
TypeError: not all arguments converted during string formatting
Enter fullscreen mode Exit fullscreen mode

Solution – The best way to resolve this issue is to convert the number into an integer or floating-point if we perform a modulus operation.

# Check even or odd scenario
number= (input("Enter a Number: "))
if(int(number) % 2):
    print("Given number is odd")
else:
    print("Given number is even")

# Output
Enter a Number: 5
Given number is odd
Enter fullscreen mode Exit fullscreen mode

Incorrect formatting and substitution of values during string interpolation

In this example, we are performing a string interpolation by substituting the values to the string specifiers. If you notice clearly, we are passing an extra value country without providing the specifier for which Python will throw a not all arguments converted during string formatting error.

name ="Jack"
age =20
country="India"

print("Student %s is %s years old "%(name,age,country))

# Output
Traceback (most recent call last):
  File "c:\Projects\Tryouts\listindexerror.py", line 5, in <module>
    print("Student %s is %s years old "%(name,age,country))
TypeError: not all arguments converted during string formatting
Enter fullscreen mode Exit fullscreen mode

*Solution – * You could resolve the issue by matching the number of specifiers and values, as shown above.

name ="Jack"
age =20
country="India"

print("Student %s is %s years old and he is from %s "%(name,age,country))

# Output
Student Jack is 20 years old and he is from India 
Enter fullscreen mode Exit fullscreen mode

Mixing different types of format specifiers

The major issue in the below code is mixing up two different types of string formatting. We have used {} and % operators to perform string interpolation, so Python will throw TypeError in this case.

# Print Name and age of Student
name = input("Enter name : ")
age = input("Enter Age : ")

# Print Name and Age of user
print("Student name is '{0}'and Age is '{1}'"% name, age)

# Output
Enter name : Chandler
Enter Age : 22
Traceback (most recent call last):
  File "c:\Projects\Tryouts\listindexerror.py", line 6, in <module>
    print("Student name is '{0}'and Age is '{1}'"% name, age)
TypeError: not all arguments converted during string formatting
Enter fullscreen mode Exit fullscreen mode

Solution – ** The **% operator will be soon deprecated, instead use the modern approach {} with .format() method as shown below.

The .format() method replaces the values of {} with the values specified in .format() in the same order mentioned.


# Print Name and age of Student
name = input("Enter name : ")
age = input("Enter Age : ")

# Print Name and Age of user
print("Student name is '{0}'and Age is '{1}'".format(name, age))

# Output
Enter name : Chandler
Enter Age : 22
Student name is 'Chandler'and Age is '22'
Enter fullscreen mode Exit fullscreen mode

The post Python typeerror: not all arguments converted during string formatting appeared first on ItsMyCode.

Discussion (0)