directory
- The difference between byte and character
- 1. Byte concept
- 2. Character concepts
- 3. String concepts
- 4. The concept of bytes
- The STR/bytes/bytearray distinction
- String is converted to bytes/bytearray
- 1. String is encoded into bytes
- 2. Bytes are decoded and converted to a string
- Four. Guess you like it
Recommended path for learning Python: Python Learning Directory >> Python Basics
The difference between byte and character
Before explaining the differences between bytearray/bytes / **string **, it’s important to understand the differences between bytes and characters.
1. Byte concept
A Byte is a unit of measurement used in computer information technology to measure storage capacity. A string of binary digits treated as a unit is a small unit of information. The most common byte is the octet, that is, it contains eight bits of binary number;
-
A bit is the smallest unit of data storage in a computer. 11001100 is an eight-bit binary number.
-
** bytes are the basic unit of data processing in computers, conventionally represented by a capital B, 1B (byte) = 8 bits; 阿鲁纳恰尔邦
1 KB = 1024 B(bytes); 1 MB = 1024 KB; (2^10 B) 1 GB = 1024 MB; (2^20 B) 1 TB = 1024 GB; (2^30 B)
2. Character concepts
Characters refer to letters, digits, characters, and symbols used in the computer, including: 1, 2, 3, A, B, C, and ~! · # $%…… — * () — + etc.;
- In utF-8 encoding, a Chinese character occupies 3 bytes.
- Under general GBK encoding, a Chinese character occupies 2 bytes;
3. String concepts
A string is a sequence of characters, an abstract concept that cannot be stored directly on a hard disk — bytes are meant for computers to read, transfer, or save. In Python, text in programs is represented as strings;
4. The concept of bytes
A byte string is a sequence of bytes that can be stored directly on the hard disk. A byte string is shown to the computer. The mapping between them is called encoding/decoding — strings are to be seen, to be manipulated;
#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python bytearray/bytes/string difference. Py @time :2021/04/30 08:00 @motto: A thousand miles without a small stream without a river or sea. The wonderful program life needs to accumulate unremittingly! """ if __name__ == "__main__": # string STR to bytes s = 'ape say python' b = s.encode() # encode, Utf-8 print(b) print(type(b)) # byte bytes convert STR b = b'\xe7\x8c\ XBF \xe8\xaf\ XB4python '. Decode (encoding=' utF-8 ') # Print (b) print(type(b)) "" b" \xe7\x8c\ XBF \xe8\xaf\xb4python' <class 'bytes'>Copy the code
The STR/bytes/bytearray distinction
1. STR is character data (e.g., text for humans), bytes and bytearray are byte data (e.g., binary data for computers), which are sequences that can be iterated over.
2. STR and bytes are immutable sequences that are essentially recreated using the generic functions of STR, such as find, replace, and islower. Bytearray is a mutable sequence that can be modified in bytes.
3. Both bytes and bytearray can use STR generic functions, such as find, replace, islower, etc.
4. The default STR in Python 3.x is encoded in Unicode format, such as the UTF-8 character set.
String is converted to bytes/bytearray
1. String is encoded into bytes
#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python bytearray/bytes/string difference. Py @time :2021/04/30 08:00 @motto: A thousand miles without a small stream without a river or sea. The wonderful program life needs to accumulate unremittingly! """ if __name__ == "__main__": S = "https://www.codersrc.com" # convert string to byte object b2 = bytes(s, B3 = str.encode(s) b4 = s.encode() print(b3) Print (type(b3)) print(b4) print(type(b4)) b'https://www.codersrc.com' <class 'bytes'> b'https://www.codersrc.com' <class 'bytes'> '''Copy the code
2. Bytes are decoded and converted to a string
#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python bytearray/bytes/string difference. Py @time :2021/04/30 08:00 @motto: A thousand miles without a small stream without a river or sea. The wonderful program life needs to accumulate unremittingly! """ if __name__ == "__main__": Decode (b, s2, utF-8, utF-8) S3 = b.code () print(s2) print(s3)"Copy the code
TypeError: String argument without an encoding TypeError: String argument without an encoding
#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python bytearray/bytes/string difference. Py @time :2021/04/30 08:00 @motto: A thousand miles without a small stream without a river or sea. The wonderful program life needs to accumulate unremittingly! >>> TypeError: string argument without an encoding. TypeError: String argument without an encodingCopy the code
Four.Guess you like
- The Python for loop
- The Python string
- The Python list
- The Python tuple tuple
- Python dictionary dict
- Python conditional derivations
- Python list derivations
- Python dictionary derivations
- Python function declarations and calls
- Python variable argument *argc/**kargcs
- Python anonymous function lambda
- Python return logic determines expressions
- Python string/list/tuple/dictionary conversions
- Python local and global variables
- The Python type function is different from the isinstance function
- Python is differs from ==
- Python mutable and immutable data types
- Shallow and deep copies of Python
Python bytearray/bytes/string
This article is published by the blog – Ape Say Programming Ape Say programming!