TypeError: not all arguments converted during string formatting python

765,406

Solution 1

You're mixing different format functions.

The old-style % formatting uses % codes for formatting:

'It will cost $%d dollars.' % 95

The new-style {} formatting uses {} codes and the .format method

'It will cost ${0} dollars.'.format(95)

Note that with old-style formatting, you have to specify multiple arguments using a tuple:

'%d days and %d nights' % (40, 40)

In your case, since you're using {} format specifiers, use .format:

"'{0}' is longer than '{1}'".format(name1, name2)

Solution 2

The error is in your string formatting.

The correct way to use traditional string formatting using the '%' operator is to use a printf-style format string (Python documentation for this here: http://docs.python.org/2/library/string.html#format-string-syntax):

"'%s' is longer than '%s'" % (name1, name2)

However, the '%' operator will probably be deprecated in the future. The new PEP 3101 way of doing things is like this:

"'{0}' is longer than '{1}'".format(name1, name2)

Solution 3

For me, This error was caused when I was attempting to pass in a tuple into the string format method.

I found the solution from this question/answer

Copying and pasting the correct answer from the link (NOT MY WORK):

>>> thetuple = (1, 2, 3)
>>> print "this is a tuple: %s" % (thetuple,)
this is a tuple: (1, 2, 3)

Making a singleton tuple with the tuple of interest as the only item, i.e. the (thetuple,) part, is the key bit here.

Solution 4

In my case, it's because I need only a single %s, i missing values input.

Solution 5

In addition to the other two answers, I think the indentations are also incorrect in the last two conditions. The conditions are that one name is longer than the other and they need to start with 'elif' and with no indentations. If you put it within the first condition (by giving it four indentations from the margin), it ends up being contradictory because the lengths of the names cannot be equal and different at the same time.

    else:
        print ("The names are different, but are the same length")
elif len(name1) > len(name2):
    print ("{0} is longer than {1}".format(name1, name2))
Share:
765,406

Related videos on Youtube

user2652300
Author by

user2652300

Updated on August 13, 2021

Comments

  • user2652300
    user2652300 almost 3 years

    The program is supposed to take in two names, and if they are the same length it should check if they are the same word. If it's the same word it will print "The names are the same". If they are the same length but with different letters it will print "The names are different but the same length". The part I'm having a problem with is in the bottom 4 lines.

    #!/usr/bin/env python
    # Enter your code for "What's In (The Length Of) A Name?" here.
    name1 = input("Enter name 1: ")
    name2 = input("Enter name 2: ")
    len(name1)
    len(name2)
    if len(name1) == len(name2):
        if name1 == name2:
            print ("The names are the same")
        else:
            print ("The names are different, but are the same length")
        if len(name1) > len(name2):
            print ("'{0}' is longer than '{1}'"% name1, name2)
        elif len(name1) < len(name2):
            print ("'{0}'is longer than '{1}'"% name2, name1)
    

    When I run this code it displays:

    Traceback (most recent call last):
      File "program.py", line 13, in <module>
        print ("'{0}' is longer than '{1}'"% name1, name2)
    TypeError: not all arguments converted during string formatting
    

    Any suggestions are highly appreciated.

  • cfi
    cfi over 8 years
    scnr: "will probably be deprecated in the future" did not happen so far (Python 3.5). The old '%' syntax wasn't deprecated in 3.1 and only in 3.2 logging module learned how to format with the new style {}. And suddenly 3.5 brings PEP 461: % formatting for bytes. This makes me think the % remains for a long time to come.
  • Lenar Hoyt
    Lenar Hoyt about 8 years
    % is more concise. Glad it stays with us.
  • Alex
    Alex over 7 years
    I would rather convert the tuple to a string using one of the following statements: print("this is a tuple: %s" % str(thetuple)) or print("this is a tuple: %s" % repr(thetuple))
  • JinSnow
    JinSnow over 7 years
    in python 3.6: f"'It will cost ${your_variable} dollars."
  • chevydog
    chevydog over 6 years
    I concur. % is more concise and removing would add no benefit to the language.
  • Jatin Patel - JP
    Jatin Patel - JP over 5 years
    @ParisNakitaKejser, so how to get input param for single %s ?
  • Mark Moretto
    Mark Moretto over 4 years
    @LenarHoyt How do you feel about f-strings? I can't imagine that is "'%s' is longer than '%s'" % (name1, name2) more concise than f"'{name1}' is longer than '{name2}'"
  • Darko Kolev
    Darko Kolev over 3 years
    I'm all for f-strings, but they're too new and you can't use them on project that is more than a year old
  • Mahmood Dehghan
    Mahmood Dehghan over 3 years
    @JinSnow it does not need the $ sign.
  • musava_ribica
    musava_ribica over 3 years
    he added it for readability of the result, although not gramatically correct (also including the single quote at the beginning). The result would be 'It will cost $50 dollars.
  • Lenar Hoyt
    Lenar Hoyt over 3 years
    f-strings are often more concise, but there are some instances where % is still superior, e.g. when you want to do multi-line computations for the arguments or computations containing other strings.
  • Sam Daniel
    Sam Daniel over 2 years
    suppliment to @JinSnow comment.. if you want the variable name also to be printed, f"'It will cost {your_variable=} dollars."
  • pppery
    pppery almost 2 years
    This doesn;'t actually answer the question.