开发者

What is the difference between % and , in a string?

开发者 https://www.devze.com 2023-02-26 02:39 出处:网络
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):

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消