Loading video player…

Handling String Data Types

Resources mentioned in this lesson:

00:00 In the previous lesson, I started a new section on built-in functions for creating data types. I began with numerics and then snuck Booleans into the mix.

00:10 In this lesson, I’ll continue on the data type journey, this time looking at strings and characters. Python handles text through the string data type. A lot of objects support being converted to strings, and there are two different ways to do that through the str() function, usually pronounced string, and the repr() function.

00:30 The str() function is meant for human-readable content, while the repr() function is meant to produce a string which, when copied into the REPL, would clone the object being described.

00:40 Unlike other programming languages, Python doesn’t really have a concept of a character. Characters aren’t separate data types, merely strings of length one.

00:49 That’s a vast oversimplification because of Unicode encoding, but let’s stick with it for now. Python does provide two functions for dealing with these single-length strings that from here on in I’m going to call characters for want of a better word.

01:03 These functions help you go from ASCII or Unicode points to strings and back again. To go along with all of this, there’s also a built-in function called ascii() that converts an object using only ASCII, escaping anything Unicode.

01:17 Once more, back to the REPL, this time to explore strings. Just like how you can use the number functions to do conversions to their types, you can use the str() function to convert things to strings.

01:31 Here, I’ve converted an integer to a string

01:36 and now a float. The repr() function works similarly.

01:41 And now once more. So you might be asking why have these two functions when they do the same thing? Well, they’re doing the same thing because I’m converting very simple objects.

01:52 Integers and floats are simple enough that str() and repr() do the same operation. But if I create a more complicated object, you can see the difference.

02:02 I’ve imported the datetime module so that I can create a datetime object.

02:12 There’s a day. And when you evaluate that day in the REPL, you see the result that would be used to construct a new object. But when I pass day into str(), I get a fairly readable datetime string using the ISO 8601 date standard.

02:30 Note that this is exactly what happens when you print something.

02:34 The print() function converts the object you pass it to a string object using the str() function and then displays the result on the screen.

02:43 If I use repr() instead, I get something completely different. This represents the datetime object as you might see it in the REPL.

02:52 If I copy and paste the contents of this string into the next command spot, I get back an equivalent datetime object. In fact, you’ll notice this is exactly what was output when I evaluated day in the REPL.

03:05 That’s what the REPL does. It evaluates an expression and returns the repr() version of the result. If you create your own objects, Python doesn’t enforce this format programmatically, but it is best practice for your repr() value to return a string that could be used as a constructor for the object, while the str() value is whatever is useful to your users.

03:26 Let’s take a quick tangent and traipse behind the scenes a bit. Everything in Python is an object.

03:34 Little Easter egg for any podcast listeners. And objects have special methods that are responsible for type conversion. When you use the str() or repr() functions, they are actually calling these special methods.

03:45 Let me show you what I mean.

03:49 I’m creating a new class called Cat.

03:52 And inside of it, I’m defining the .__str__() method. This is one of those special methods, and seeing as double underscore is a little hard to say, they often get referred to as dunder methods. The .__str__() method is what gets called when an instance of the object is handed off to the str() function.

04:12 I’m going to return Meow.

04:15 And as you might guess, the .__repr__() method is what gets called on a Cat when it’s passed into the repr() call.

04:25 Here, I’ve followed best practices and returned a string that could be used to construct a new object. My object’s pretty simple and has no arguments, so it really is just the instantiation.

04:37 Let me create a cat.

04:40 When I convert it with str(), I hear Meow. Well, I don’t hear it.

04:48 Same goes with print(). And when I convert with repr(), I get a string that could be used to instantiate the object. This is the same result I get if I evaluate my kitty in the REPL.

05:01 So the next time you examine an object in the REPL or print it out, you’ll know why it showed what it did.

05:08 Python strings use Unicode by default. Unicode is a massive table that maps a number to the characters you see in the text on your screen. It supports a huge number of languages, as well as all of those emojis you probably use on a regular basis, and even the ones you don’t even know exist.

05:25 The first part of the Unicode table is actually the ASCII table to keep backward compatibility with all those systems that predate the use of more complex text encoding.

05:35 The numeric value associated with a character is called a code point, and the ord() function takes a character and returns the corresponding code point.

05:45 The code point for capital A is 65, which is also its ASCII value because this letter has been used in computing almost from the beginning. The code point for little x is 120, also an ASCII value.

06:00 Let’s try something a little newer.

06:03 That’s way past the ASCII limit of 255, or 127 if you’re really old school. This character is definitely up into the Unicode-only space. The chr() function allows you to go in the other direction.

06:18 chr(65) gives you A, and 120 gives you x.

06:25 And there’s our snake coming back. Unicode is a deep and complicated thing. When I finish in the REPL, I’ll point you at some places where you can dig in some more, if you’re interested.

06:36 Before that, though, one last quick function.

06:42 ascii() returns a printable representation of an object escaping any non-ASCII characters. This function uses the repr() version of an object.

06:51 For a string like I’ve done here, the repr() and str() versions are the same. Either way, you get the snake Unicode escaped out.

06:59 So Hiss in ASCII, and then the escape code afterwards. For more on .__str__() and .__repr__(), see this video course or tutorial.

07:10 Or if you’d like to learn the ins and outs of Unicode, these two are deep dives on the subject.

07:17 Still in the data types land, next I’ll move from text to bytes.

Become a Member to join the conversation.