From the course: Effective Serialization with Python

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

str and Bytes

str and Bytes - Python Tutorial

From the course: Effective Serialization with Python

Start my 1-month free trial

str and Bytes

- [Instructor] One of the big changes between Python 2 and Python 3 was how we work with Unicode. In Python 3, we have two types, str and Bytes. Str is the Unicode string, where every character maps to a code point. Byte is a sequence of bytes, which can be an encoding of a str. Let's take the string der fluss, which means the river in German. I'm going to start Ipython, and I'm going to say s equal der fluss. The length of s is eight, and we can see eight characters in the string. Converted to bytes, we're going to do s.encode, and we say the encoding utf-8 is the default. And when we look at b, we see that the last character, the sharp s, or the str is converted to two bytes. And the length of the encoded strings is nine bytes versus eight characters. To get str from a sequence of bytes, you need to use the decode method s2 equal b.decode, and we say the encoding is utf-8. And we see that we got the same string.…

Contents