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

When formatting the output of a string, one of the methods is as follows:

for value in ['A', 'B', 'C']:
    print('output: %s' % value)

When I print the running status of the sub process, print() I found several strings in the list, but the error is as follows:

TypeError: not all arguments converted during string formatting

Not all the parameters are converted during the string formatting period. I looked at the location of the error. It turned out that % was wrongly typed, and it became $, and I didn’t know the reason at first. I saw this error for the first time and sent this information to everyone for reference. This is a low-level mistake

Supplement: several ways of string output

Direct print() output

print('str1', 'str2', 'str3', '...')

'%s' % value Placeholder output

# 1. Single
>>> name = 'Jason'
>>> print('name: %s' % name)
name: Jason
# 2.Multiple
>>> name1, name2 = 'Jason', 'Bob'
>>> print('name1: %s; name2: %s' % (name1, name2))
name1:, Jason; name2: Bob
# Output order can also be customized
>>> print('name1: %s; name2: %s' % (name2, name1))
name1: Bob; name2: Jason

When outputting multiple string variables, multiple variables after % need to be enclosed in parentheses, otherwise the error of insufficient parameters will be reported

TypeError: not enough arguments for format string

format() format output

# 1. Single
>>> '{}'.format('world')
# 2. Multiple
>>> word1, word2 = 'hello', 'world'
>>> hello_world = '{}, {}'.format(word1, word2)
>>> hello_world
'hello, world'
# Output order can also be customized
>>> hello_world = '{1}, {1}'.format(word1, word2)
>>> hello_world
'world, world'

When output at specified position, all {} must have subscripts of parameters, otherwise it will not be able to convert from manual field number to automatic field number valueerror error:

ValueError: cannot switch from manual field specification to automatic field numbering

It must also be within the subscript range, otherwise an overrun indexerror error will be reported

IndexError: Replacement index 2 out of range for positional args tuple

Read More: