close

[Solved] TypeError: not all arguments converted during string formatting

Hello Guys, How are you all? Hope You all Are Fine. Today I am trying to Compare input value with another value. Here is error that I am facing TypeError: not all arguments converted during string formatting in Python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How TypeError: not all arguments converted during string formatting Error Occurs ?

I am trying to Compare input value with another value. Here is error that I am facing.

Enter Your Age: 17
Traceback (most recent call last):
  File "f:\Python Script\Python\2021\temp.py", line 9, in <module>
    print ("Your Age is '{0}' Which is Less Than '{1}' So That You Are Not Eligible"% age, "18")
TypeError: not all arguments converted during string formatting

Here is my simple Code.

age = input("Enter Your Age: ")
if age <= "18":
    print ("Your Age is '{0}' Which is Less Than '{1}' So That You Are Not Eligible"% age, "18")
else:
    print("Eligible")

How To Solve TypeError: not all arguments converted during string formatting Error ?

  1. How To Solve TypeError: not all arguments converted during string formatting Error ?

    To Solve TypeError: not all arguments converted during string formatting Error If You Are Using {} then You have to use .format. Second solution is For String formatting Use the ‘%’ operator is to use a printf-style format string. Now, Your error must be solved.

  2. TypeError: not all arguments converted during string formatting

    To Solve TypeError: not all arguments converted during string formatting Error If You Are Using {} then You have to use .format. Second solution is For String formatting Use the ‘%’ operator is to use a printf-style format string. Now, Your error must be solved.

Solution 1: use .format

If You Are Using {} then You have to use .format Just Like This.

"Your Age is '{0}' Which is less than '{1}' So That You are not eligible ".format(params1, params2)

Now, Your Error must be solved. Here is Full Code.

age = input("Enter Your Age: ")
if age <= "18":
    print ("Your Age is '{0}' Which is less than '{1}' So That You are not eligible ".format(age, 18))
else:
    print("Eligible")

This is Output Of Above code.

Enter Your Age: 17
Your Age is '17' Which is less than '18' So That You are not eligible 

Solution 2: use the ‘%’ operator

For String formatting Use the ‘%’ operator is to use a printf-style format string.

"Your Age is '%s' Which is less than '%s' So That You are not eligible" % (age, 18)

Now, Your error must be solved.

Solution 3: Use f-strings

Just use f-strings Here is syntax.

username = "Harshil"
userage = 24
print(f"Username is {username} and Age is {userage}.")

Username is Harshil and Age is 24. # OUTPUT

Now, your error must be solved.

Summary

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?

Also, Read

Leave a Comment