What is the difference between % and , in a string?
I am using a String
in Python and need to update it
line = ''
byte_data = 0
What is the difference between these two syntaxes (what they do):
line += "%c" 开发者_运维问答% byte_data
line += "%c", byte_data
The former adds \x00
to the string, and the latter results in a TypeError
.
The difference is that one works and one does not.
>>> line = ''
>>> byte_data = 0
>>> line += "%c" % byte_data
>>> line
'\x00'
>>> line += "%c", byte_data
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
line += "%c", byte_data
TypeError: cannot concatenate 'str' and 'tuple' objects
>>>
I'm not quite sure where you've seen comma used to populate strings, but unfortunately that will result in a TypeError.
精彩评论