In this lesson, you’ll take a tour of Python built-in functions that can help you with ASCII, Unicode, and conversion between numeric representations. These can be used individually or logically grouped together based on their purpose. Here are links to the Python 3 documentation for built-in functions that relate to numbering systems and character encoding:
Using Built-In Functions
00:00 In the previous lesson, I talked about digraph, ligatures, and other ways of combining characters in Unicode. In this lesson, I’m going to give you a tour of useful built-in functions when you’re dealing with text and code points.
The first function I’m going to show you is
ascii() returns the
repr() compatible string. This means what comes out of it is something that could be used in
repr() or in
eval() to get the contents.
Passing in a simple string that contains ASCII characters results in a string that is quoted. Notice in here that the single ticks are actually part of the string. Because this is for ASCII only, anything 128 or higher in the code points gets converted into an escape sequence. Again, because this is a
repr() compatible string, even the escape sequence gets escaped, so you get the
\\ in here.
And passing in an integer value gets the quoted decimal returned. Next up is
bin() gives you the string representation of a binary number.
0 is easy.
9 decimal gets converted. And
E6 hex gets converted as well. The
bytes() function returns a
bytes object—raw byte data.
01:30 This function takes a variety of inputs. If you pass in an iterable it will construct the byte data based on the content.
Those are the decimal values for the ASCII code points for the word
'hello'. You get the binary data object with the ASCII values
'hello' inside of it.
Same thing going on here. You can also pass in a string specifying the encoding. If you pass in an integer, it will give you that many blank bytes. So,
bytes(5) returns five sets of null.
You can also call its companion method
.fromhex(). This gives you a byte string with the hex values from the string inside.
You’ve seen the
chr() function before—it takes an integer code point value and returns the Unicode character.
Any number can be used. The
hex() function is similar to the
bin() function, but instead of returning a string representation of a binary number, it returns the string representation of a hex number.
int() returns an integer based on what you pass in.
03:12 Passing in an integer gives you that integer. Passing in a float gives you a cut. Passing in a string containing an integer parses it and returns the integer.
If you pass in a string, you can also specify the base.
'11' in base
'11' in base
17. This can be done with anything that’s a number, including math. The companion method for
int() also allows you to use bytes as input.
04:00 Within your computer, larger numbers are stored in one of two ways: either big-endian or little-endian. This specifies the order of how the bytes are read together. In big-endian order, the order of the string is the order of the number.
So this byte string is being read as
10 in hex, which of course is
16. By contrast, little-endian byte order swaps the order around.
'0010' is being stored as
\x10\x00, which when converted from hex into decimal is
4096. Big-endian and little-endian are processor architecture choices.
04:41 Programmers generally don’t have to pay attention to them—they’re abstracted away. But depending on how the processor uses certain kinds of special data, if you’re fiddling with bits, you might need to know which order they come in.
You’ve seen the
ord() method before—this converts a character into its integer code point.
And finally, the
str() method converts its input into a string.
05:12 Passing in a string returns the string. Passing in a number converts the number into a string. Numbers are converted into decimal before the conversion to string happens.
05:24 And finally, you can specify the encoding type in order to decode binary information into a string. UTF-8 isn’t the only way to go. Next up, I’ll show you a couple other encodings.
Become a Member to join the conversation.