Python-bytes-bytearray

Source: Internet
Author: User
Tags throw exception

Bytes, bytearrybytes (immutable byte sequence) bytearray (variable byte genus) bytes initialization
#空bytesprint(bytes())    #输出:b‘‘#指定字节的bytesprint(bytes(2))   #输出:b‘\x00\x00‘#bytes(iterable_of_ints)->bytesprint(bytes([0,255]))   #输出b‘\x00\xff‘#bytes(string,encoding[,errors])->bytes等价于string.encode()print(bytes(‘abc‘,encoding=‘utf8‘)) #输出b‘abc‘#bytes(bytes_or_buffer)->immutable copy of bytes_or_buffer从一个字节序列或者buffer复制出一个新的不可变bytesa=b‘abcss‘b=bytes(a)print(b)                #输出:b‘abcss‘print(id(a))            #输出:4368131856print(id(b))            #输出:4368131856#说明以上复制并不是重新开辟一块内存空间,而是同时指向这块内存空间#使用b前缀定义print(b‘asd‘)           #输出:b‘asd‘print(b‘\x41\x61‘)      #输出:b‘Aa‘
Bytes operation
#bytes和str类型类似,均为不可变,很多方法一样print(b‘abcsda‘.replace(b‘a‘,b‘z‘))    #输出:b‘zbcsdz‘print(b‘abc‘.find(b‘b‘))               #输出:1#类方法(string必须是2个字符的16进制形式,空格被忽略)print(bytes.fromhex(‘6162 09 6a 6b00‘))     #b‘ab\tjk\x00‘#hex(),返回16进制表示的字符串print(‘abc‘.encode().hex())             #输出:616263        #索引print(b‘abcdef‘[2])                     #输出:99
ByteArray initialization
#空bytearrayprint(bytearray())              #输出:bytearray(b‘‘)#bytearry(int)指定字节的bytearray,被0填充print(bytearray(5))             #输出:bytearray(b‘\x00\x00\x00\x00\x00‘)#bytearray(iterable_of_ints)->bytearrayprint(bytearray([0,255]))       #输出:bytearray(b‘\x00\xff‘)#bytearray(string,encoding[,errors])->bytearray近似string.encode()print(bytearray(‘abc‘,encoding=‘utf8‘)) #输出bytearray(b‘abc‘)#bytearray(bytes_or_buffer)->immutable copy of bytes_or_buffer从一个字节序列或者buffer复制出一个新的可变的bytearray对象a=b‘abcss‘b=bytearray(a)print(b)                #输出:bytearray(b‘abcss‘)print(id(a))            #输出:4368131856print(id(b))            #输出:4362498768
ByteArray operation
#和bytes类型的方法相同print (ByteArray (b ' ABCSDA '). Replace (b ' A ', B ' Z ')) #输出: ByteArray (b ' Zbcsdz ') print (ByteArray (b ' abc '). Find (b ' B ')) #输出: The # class method (string must be 16 binary form of 2 characters, space is ignored) print (Bytearray.fromhex (' 6162 6a 6b00 ')) #bytearray ( B ' ab\tjk\x00 ') #hex (), returns a 16 binary representation of the string print (ByteArray (' abc '. encode ()). Hex ()) #输出: 616263# index Print (ByteArray (b ' abcdef ') ) [2]) #输出: 99#append (int) trailing an element B=bytearray () b.append (b.append) print (b) #输出: b Ytearray (b ' AC ') #insert (index,int) inserts the element at the specified index B.insert (1,98) print (b) #输出: ByteArray (b ' abc ') #extend (iterabl e_of_ints) appends an iterative set of integers to the current bytearraya=[65,66,67]b.extend (a) print (b) #输出: ByteArray (b ' abcabc ') #pop (index=- 1) Removes the element from the specified index, by default B.pop () print (b) #输出: ByteArray (b ' Abcab ') #remove (value) found the first value removed, no throw exception was found B.remove (66) Print (b) #输出: ByteArray (b ' Abcab ') #reverse () Flip B.reverse () print (b) #输出: ByteArray (b ' ACBA ') #clear () empty bytearrayb.clear () prinT (b) #输出: ByteArray (b ') 
Encoding and decoding
#字符串按照不同的字符编码encode返回字节序列bytesprint(‘abc‘.encode(encoding=‘utf8‘,errors=‘strict‘))   #输出:b‘abc‘#字节序列按照不同的字符集解码decode返回字符串print(b‘abc‘.decode(encoding="utf-8",errors=‘strict‘)) #输出:abcprint(bytearray(b‘abc‘).decode(encoding="utf-8",errors=‘strict‘))  #输出:abc

Python-bytes-bytearray

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.