byte strings python

It is a common misconception that text is ascii or utf8 or cp1252, and therefore bytes are text.
Text is only text, in the way that images are only images. 
The matter of storing text or images to disk is a matter of encoding that data into a sequence of bytes. 
There are many ways to encode images into bytes: Jpeg, png, svg, and likewise many ways to encode text, ascii, utf8 or cp1252.
Once encoding has happened, bytes are just bytes. 
Bytes are not images anymore,they have forgotten the colors they mean; 
although an image format decoder can recover that information.
Bytes have similarly forgotten the letters they used to be. 
In fact, bytes don't remember wether they were images or text at all. Only out of band knowledge 
(filename, media headers, etcetera) can guess what those bytes should mean, and even that can be wrong (in case of data corruption)
so, in python (py3), we have two types for things that might otherwise look similar; 
For text, we have str, which knows it's text; it knows which letters it's supposed to mean. 
It doesn't know which bytes that might be, since letters are not bytes. 
We also have bytestring, which doesn't know if it's text or images or any other kind of data.
The two types are superficially similar, since they are both sequences of things, but the things that they are sequences of is quite different.
Implementationally, str is stored in memory as UCS-? where the ? is implementation defined, 
it may be UCS4, UCS2 or UCS1, depending on compile time options and which codepoints are present in the represented string.

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source