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.
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.
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.
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.
Become a Member to join the conversation.