| [ Web Proxy ] |
| Viewing: https://wiki.python.org/moin/../python/platforms/../web/../../python/gui/../archive/BytesStr.html | [Back] [Original] |
Legacy Wiki Page
This page was migrated from the old MoinMoin-based wiki. Information may be outdated or no longer applicable. For current documentation, see python.org.
Python 3 uses two very different types:
bytes: intended to represent raw byte data. For more information on this type, please consult PEP 358.
str: a unicode character string
When choosing the type you want to use to work with text you have to ask yourself: do I manipulate characters or bytes (integers)? A is a character and 65 is an integer. Examples:
a network socket manipulates bytes
a text parser manipulates characters (uses lower, strip, etc. methods)
Its important to note that the bytes iterator generates integers and not characters:
>>> for item in b'abc':
... print item
97
98
99
Comparing one bytes object to another works as expected:
>>> b'xyz' == b'xyz'
True
>>> b'xyz' == b'abc'
False
However, it is important to note that the bytes type is completely distinct from the str type in Python 3, and comparisons between them do not work:
>>> b'xyz' == 'xyz'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't compare bytes and str
This should make clearly evident some incomplete transitions. But it also means that you really cant mix then very well:
>>> L = ["1", b"1"]
>>> "1" in L
True
>>> "2" in L
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't compare str and bytes
As mentioned earlier, getting an item of a bytes returns an integer, not a bytes object:
>>> b'xyz'[0] == b'x'
False
>>> b'xyz'[0]
120
bytes is mutable, and as a result, its not hashable. Among other things, this means that bytes objects cant be used as keys in dictionaries.
Hacks and workarounds for this include:
use buffer(value)
Other solutions include:
create an immutable frozenbytes type
avoid using hash
For historical information that may be useful in porting or maintaining remaining Python 2 systems, please see previous page revisions.
Back to top2026, Python Software Foundation
Made with Sphinx and Shibuya theme.
| Web Proxy Viewer | New URL | Original Page |