Hướng dẫn what is the difference between bytes and string in python? - sự khác biệt giữa byte và chuỗi trong python là gì?

Lưu ý: Tôi sẽ giải thích nhiều hơn câu trả lời của mình cho Python 3 vì kết thúc cuộc đời của Python 2 rất gần. I will elaborate more my answer for Python 3 since the end of life of Python 2 is very close.

Trong Python 3

bytes bao gồm các chuỗi các giá trị không dấu 8 bit, trong khi str bao gồm các chuỗi các điểm mã Unicode đại diện cho các ký tự văn bản từ ngôn ngữ con người.

>>> # bytes
>>> b = b'h\x65llo'
>>> type(b)

>>> list(b)
[104, 101, 108, 108, 111]
>>> print(b)
b'hello'
>>>
>>> # str
>>> s = 'nai\u0308ve'
>>> type(s)

>>> list(s)
['n', 'a', 'i', '̈', 'v', 'e']
>>> print(s)
naïve

Mặc dù bytesstr dường như hoạt động theo cùng một cách, các trường hợp của chúng không tương thích với nhau, tức là, các trường hợp bytesstr không thể được sử dụng cùng với các nhà khai thác như

>>> # concatenation
>>> b'hi' + b'bye' # this is possible
b'hibye'
>>> 'hi' + 'bye' # this is also possible
'hibye'
>>> b'hi' + 'bye' # this will fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't concat str to bytes
>>> 'hi' + b'bye' # this will also fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate str (not "bytes") to str
>>>
>>> # comparison
>>> b'red' > b'blue' # this is possible
True
>>> 'red'> 'blue' # this is also possible
True
>>> b'red' > 'blue' # you can't compare bytes with str
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'bytes' and 'str'
>>> 'red' > b'blue' # you can't compare str with bytes
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'str' and 'bytes'
>>> b'blue' == 'red' # equality between str and bytes always evaluates to False
False
>>> b'blue' == 'blue' # equality between str and bytes always evaluates to False
False
0 và
>>> # concatenation
>>> b'hi' + b'bye' # this is possible
b'hibye'
>>> 'hi' + 'bye' # this is also possible
'hibye'
>>> b'hi' + 'bye' # this will fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't concat str to bytes
>>> 'hi' + b'bye' # this will also fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate str (not "bytes") to str
>>>
>>> # comparison
>>> b'red' > b'blue' # this is possible
True
>>> 'red'> 'blue' # this is also possible
True
>>> b'red' > 'blue' # you can't compare bytes with str
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'bytes' and 'str'
>>> 'red' > b'blue' # you can't compare str with bytes
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'str' and 'bytes'
>>> b'blue' == 'red' # equality between str and bytes always evaluates to False
False
>>> b'blue' == 'blue' # equality between str and bytes always evaluates to False
False
1. Ngoài ra, hãy nhớ rằng so sánh các trường hợp bytesstr cho sự bình đẳng, tức là sử dụng
>>> # concatenation
>>> b'hi' + b'bye' # this is possible
b'hibye'
>>> 'hi' + 'bye' # this is also possible
'hibye'
>>> b'hi' + 'bye' # this will fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't concat str to bytes
>>> 'hi' + b'bye' # this will also fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate str (not "bytes") to str
>>>
>>> # comparison
>>> b'red' > b'blue' # this is possible
True
>>> 'red'> 'blue' # this is also possible
True
>>> b'red' > 'blue' # you can't compare bytes with str
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'bytes' and 'str'
>>> 'red' > b'blue' # you can't compare str with bytes
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'str' and 'bytes'
>>> b'blue' == 'red' # equality between str and bytes always evaluates to False
False
>>> b'blue' == 'blue' # equality between str and bytes always evaluates to False
False
4, sẽ luôn đánh giá thành
>>> # concatenation
>>> b'hi' + b'bye' # this is possible
b'hibye'
>>> 'hi' + 'bye' # this is also possible
'hibye'
>>> b'hi' + 'bye' # this will fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't concat str to bytes
>>> 'hi' + b'bye' # this will also fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate str (not "bytes") to str
>>>
>>> # comparison
>>> b'red' > b'blue' # this is possible
True
>>> 'red'> 'blue' # this is also possible
True
>>> b'red' > 'blue' # you can't compare bytes with str
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'bytes' and 'str'
>>> 'red' > b'blue' # you can't compare str with bytes
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'str' and 'bytes'
>>> b'blue' == 'red' # equality between str and bytes always evaluates to False
False
>>> b'blue' == 'blue' # equality between str and bytes always evaluates to False
False
5 ngay cả khi chúng chứa chính xác cùng một ký tự.

>>> # concatenation
>>> b'hi' + b'bye' # this is possible
b'hibye'
>>> 'hi' + 'bye' # this is also possible
'hibye'
>>> b'hi' + 'bye' # this will fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't concat str to bytes
>>> 'hi' + b'bye' # this will also fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate str (not "bytes") to str
>>>
>>> # comparison
>>> b'red' > b'blue' # this is possible
True
>>> 'red'> 'blue' # this is also possible
True
>>> b'red' > 'blue' # you can't compare bytes with str
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'bytes' and 'str'
>>> 'red' > b'blue' # you can't compare str with bytes
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'str' and 'bytes'
>>> b'blue' == 'red' # equality between str and bytes always evaluates to False
False
>>> b'blue' == 'blue' # equality between str and bytes always evaluates to False
False

Một vấn đề khác khi xử lý bytesstr có mặt khi làm việc với các tệp được trả về bằng hàm tích hợp

>>> # concatenation
>>> b'hi' + b'bye' # this is possible
b'hibye'
>>> 'hi' + 'bye' # this is also possible
'hibye'
>>> b'hi' + 'bye' # this will fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't concat str to bytes
>>> 'hi' + b'bye' # this will also fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate str (not "bytes") to str
>>>
>>> # comparison
>>> b'red' > b'blue' # this is possible
True
>>> 'red'> 'blue' # this is also possible
True
>>> b'red' > 'blue' # you can't compare bytes with str
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'bytes' and 'str'
>>> 'red' > b'blue' # you can't compare str with bytes
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'str' and 'bytes'
>>> b'blue' == 'red' # equality between str and bytes always evaluates to False
False
>>> b'blue' == 'blue' # equality between str and bytes always evaluates to False
False
8. Một mặt, nếu bạn muốn OT đọc hoặc ghi dữ liệu nhị phân vào/từ một tệp, hãy luôn mở tệp bằng chế độ nhị phân như 'RB' hoặc 'WB'. Mặt khác, nếu bạn muốn đọc hoặc ghi dữ liệu Unicode sang/từ một tệp, hãy lưu ý về mã hóa mặc định của máy tính của bạn, vì vậy nếu cần phải vượt qua tham số
>>> # concatenation
>>> b'hi' + b'bye' # this is possible
b'hibye'
>>> 'hi' + 'bye' # this is also possible
'hibye'
>>> b'hi' + 'bye' # this will fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't concat str to bytes
>>> 'hi' + b'bye' # this will also fail
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate str (not "bytes") to str
>>>
>>> # comparison
>>> b'red' > b'blue' # this is possible
True
>>> 'red'> 'blue' # this is also possible
True
>>> b'red' > 'blue' # you can't compare bytes with str
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'bytes' and 'str'
>>> 'red' > b'blue' # you can't compare str with bytes
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'str' and 'bytes'
>>> b'blue' == 'red' # equality between str and bytes always evaluates to False
False
>>> b'blue' == 'blue' # equality between str and bytes always evaluates to False
False
9 để tránh bất ngờ.

Trong Python 2

str bao gồm các chuỗi của các giá trị 8 bit, trong khi

s = 'Hello world' # Encoding the string into bytes bytes_obj = s.encode('ASCII') print(bytes_obj) # Output: b'Hello world'

1 bao gồm các chuỗi các ký tự unicode. Một điều cần lưu ý là str

s = 'Hello world' # Encoding the string into bytes bytes_obj = s.encode('ASCII') print(bytes_obj) # Output: b'Hello world'

1 có thể được sử dụng cùng với các toán tử nếu str chỉ bao gồm các ký tự ASCI 7 bit.

Có thể hữu ích khi sử dụng các hàm trợ giúp để chuyển đổi giữa str

s = 'Hello world' # Encoding the string into bytes bytes_obj = s.encode('ASCII') print(bytes_obj) # Output: b'Hello world'

1 trong Python 2 và giữa bytesstr trong Python 3.

Có những lúc bạn bị nhầm lẫn giữa các đối tượng byte và chuỗi. Nhưng có một số khác biệt giữa họ. Hãy thảo luận về sự khác biệt giữa chúng:

Sợi dây

Chuỗi là chuỗi các ký tự. Họ có thể đọc được con người. Chúng không thể được lưu trữ trực tiếp trên đĩa, bạn phải mã hóa chúng thành định dạng có thể đọc được bằng máy là byte.

  • s = 'Hello world' # Encoding the string into bytes bytes_obj = s.encode('ASCII') print(bytes_obj) # Output: b'Hello world'

    9

Mặc định cho các lỗi là 'nghiêm ngặt', có nghĩa là các lỗi mã hóa làm tăng unicodeerror.

Có nhiều hình thức mã hóa khác nhau như PNG, MP3, ASCII, UTF-8, v.v. được sử dụng để thể hiện hình ảnh, âm thanh, văn bản, v.v. bằng byte. Kỹ thuật mặc định là

# Byte Object bytes_obj = b'Hello world' # Decoding the bytes into string s = bytes_obj.decode('ASCII') print(s) # Output: 'Hello world'

0. Hãy lấy một ví dụ để chuyển đổi một chuỗi thành byte:

s = 'Hello world' # Encoding the string into bytes bytes_obj = s.encode('ASCII') print(bytes_obj) # Output: b'Hello world'

Trong ví dụ trên, chúng tôi đã chuyển đổi chuỗi thành byte bằng phương pháp

# Byte Object bytes_obj = b'Hello world' # Decoding the bytes into string s = bytes_obj.decode('ASCII') print(s) # Output: 'Hello world'

1. Phương thức

# Byte Object bytes_obj = b'Hello world' # Decoding the bytes into string s = bytes_obj.decode('ASCII') print(s) # Output: 'Hello world'

1 lấy loại mã hóa làm đối số. Ở đây,

# Byte Object bytes_obj = b'Hello world' # Decoding the bytes into string s = bytes_obj.decode('ASCII') print(s) # Output: 'Hello world'

3 đại diện cho chuỗi trong byte ở dạng ASCII. Phương thức

# Byte Object bytes_obj = b'Hello world' # Decoding the bytes into string s = bytes_obj.decode('ASCII') print(s) # Output: 'Hello world'

1 trả về đối tượng byte.

Đối tượng byte

Các đối tượng byte là các chuỗi bất biến của byte, nghĩa là các số nguyên trong phạm vi 0 đến 255. Byte có thể được lưu trữ trực tiếp trên đĩa. Chúng có thể đọc được bằng máy, bạn phải giải mã chúng thành định dạng có thể đọc được của con người là một chuỗi. Nếu bạn muốn nó trở lại dạng ban đầu thì bạn phải giải mã nó.

  • # Byte Object bytes_obj = b'Hello world' # Decoding the bytes into string s = bytes_obj.decode('ASCII') print(s) # Output: 'Hello world'

    5

Hãy lấy một ví dụ để chuyển đổi byte thành chuỗi:

# Byte Object bytes_obj = b'Hello world' # Decoding the bytes into string s = bytes_obj.decode('ASCII') print(s) # Output: 'Hello world'

Trong ví dụ trên, chúng tôi đã chuyển đổi các byte thành các chuỗi bằng phương pháp

# Byte Object bytes_obj = b'Hello world' # Decoding the bytes into string s = bytes_obj.decode('ASCII') print(s) # Output: 'Hello world'

6. Phương thức

# Byte Object bytes_obj = b'Hello world' # Decoding the bytes into string s = bytes_obj.decode('ASCII') print(s) # Output: 'Hello world'

6 lấy loại mã hóa làm đối số. Ở đây,

# Byte Object bytes_obj = b'Hello world' # Decoding the bytes into string s = bytes_obj.decode('ASCII') print(s) # Output: 'Hello world'

8 đại diện cho chuỗi ở dạng ASCII. Phương thức

# Byte Object bytes_obj = b'Hello world' # Decoding the bytes into string s = bytes_obj.decode('ASCII') print(s) # Output: 'Hello world'

6 trả về một chuỗi.

Các đối tượng giống như byte có thể được sử dụng trong các hoạt động khác nhau và phải ở dạng nhị phân như truyền tệp, lập trình ổ cắm, v.v.

Sự kết luận

Trong bài viết này, bạn đã học được sự khác biệt giữa các đối tượng byte và chuỗi trong Python. Chúng tôi cũng đã đề cập đến các phương pháp

# Byte Object bytes_obj = b'Hello world' # Decoding the bytes into string s = bytes_obj.decode('ASCII') print(s) # Output: 'Hello world'

1 và

# Byte Object bytes_obj = b'Hello world' # Decoding the bytes into string s = bytes_obj.decode('ASCII') print(s) # Output: 'Hello world'

6. Mã hóa và giải mã là hoạt động nghịch đảo. Trước khi lưu trữ dữ liệu trên máy tính, trước tiên bạn phải mã hóa nó. Trước khi đọc dữ liệu từ máy tính, trước tiên bạn phải giải mã nó.

Có bao nhiêu byte là một chuỗi trong Python?

Kể từ Python 3, loại STR sử dụng biểu diễn unicode.Các chuỗi Unicode có thể mất tới 4 byte trên mỗi ký tự tùy thuộc vào mã hóa, đôi khi có thể tốn kém từ góc độ bộ nhớ.up to 4 bytes per character depending on the encoding, which sometimes can be expensive from a memory perspective.

Là một chuỗi một byte?

Chuỗi byte là một mảng byte có độ dài cố định.A byte là một số nguyên chính xác trong khoảng từ 0 đến 255.Một chuỗi byte có thể bị thay đổi hoặc bất biến.....

Một byte trong Python là gì?

Python byte () Chúng tôi sử dụng phương thức python byte () để thao tác dữ liệu nhị phân trong chương trình.Byte là một đơn vị thông tin kỹ thuật số thường bao gồm 8 bit mỗi bit bao gồm 0 hoặc 1. A byte là thuật ngữ kiến trúc máy tính để lưu trữ bộ nhớ mã hóa một ký tự duy nhất của văn bản trong máy tính.a digital information unit that typically consists of 8 bits each of which consists of a 0 or 1. A byte is a computer architecture term for memory storage that encodes a single character of text in a computer.

Có bao nhiêu byte trong chuỗi?

Nhưng những gì về một chuỗi?Một chuỗi bao gồm: tiêu đề đối tượng 8 byte (đồng bộ hóa 4 byte và bộ mô tả loại 4 byte)8-byte object header (4-byte SyncBlock and a 4-byte type descriptor)