Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 在python中创建字节字符串_Python 2.7_Cmd - Fatal编程技术网

Python 2.7 在python中创建字节字符串

Python 2.7 在python中创建字节字符串,python-2.7,cmd,Python 2.7,Cmd,我试图创建一个字节字符串,但它似乎只是一个常规字符串。我做错了什么 如果您试图在Python2中创建一个字节数组,它被称为bytearray,意思是“你好”==b“你好” 试试这个: >>> f = b'f' >>> type(f) <type 'str'> 您的bytestring创建正确;仅仅因为它等于一个字符串并不意味着这是错误的。我不会回答,因为我不知道bytestring是如何工作的,但是比较bytestring和charstring

我试图创建一个字节字符串,但它似乎只是一个常规字符串。我做错了什么


如果您试图在Python2中创建一个字节数组,它被称为
bytearray
,意思是“你好”==b“你好”

试试这个:

>>> f = b'f'
>>> type(f)
<type 'str'>

您的bytestring创建正确;仅仅因为它等于一个字符串并不意味着这是错误的。我不会回答,因为我不知道bytestring是如何工作的,但是比较bytestring和charstring(通常)会给出
True
@HyperNeutrino updated,我希望byteStr的打印在第一个引号之前显示a b,而等价性测试将显示a bfail@HyperNeutrino我还看到,当我运行type(byteStr)时,我会返回。是吗?@anonymousPerson……谢谢你的随机下载,我明白了。我下面的教程使用的是python3,它解释了这种差异。非常感谢。作为一个快速的后续问题,为什么我的示例中的type(uftStr)会返回而不是?似乎.encode('utf-8')函数不起作用
>>> f = b'f'
>>> type(f)
<type 'str'>
>>> h = u'f'
>>> f == h
True
>>> type(h)
>>> <type 'unicode'>