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')
'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:
- Python TypeError: not all arguments converted during string formatting [Solved]
- Python Time Module timestamp, Time string formatting and Conversion (13-bit timestamp)
- Typeerror in Python regular expression: expected string or bytes like object
- Python TypeError: coercing to Unicode: need string or buffer, NoneType found
- [Solved] ValueError: only one element tensors can be converted to Python scalars
- Python time tuples are converted to timestamps, strings
- Pytorch Error: RuntimeError: value cannot be converted to type float without overflow: (0.00655336,-0.00
- [How to Solve] Python TypeError: ‘int‘ object is not subscriptable
- [Solved] ERROR: pip‘s dependency resolver does not currently take into account all the packages that are inst
- [Solved] TypeError: Object of type ‘bytes’ is not JSON serializable
- Python ValueError: only 2 non-keyword arguments accepted
- TypeError: Decimal type object is not a JSON serialization solution
- [Solved] main.py: error: the following arguments are required:
- [Solved] error: unrecognized arguments: –multiprocessing-fork
- [Solved] python-sutime Error: the JSON object must be str, bytes or bytearray, not ‘java.lang.String‘
- Windows10 DOTA_devkit Error: TypeError: ‘>=‘ not supported between instances of ‘NoneType‘ and ‘str‘
- [Solved] Python Error: TypeError: write() argument must be str, not bytes
- The Python DOM method iterates over all the XML in a folder
- [Solved] Error: [email protected]: wrong number of arguments (given 1, expected 0)
- Python PIP TypeError: expected str, bytes or os.PathLike object, not int