Loading video player…

Converting Data

00:00 In the previous lesson, I introduced the input() function. In this lesson, I’ll show you how to convert its string responses to other data types.

00:08 When you ask the user to type something in, they’re always typing text, even if that text is numeric digits. So if your program actually needs an integer or a float, then you have to convert the string into another data type.

00:21 You do this with data type constructors. Let’s head back to the REPL to see how they work.

00:28 While converting between data types can be important when dealing with user input, it can also be used generally. So before getting to the input case, let’s just do some regular old data type conversion.

00:40 Most of the conversion I’m going to show you is from strings to something else. So let’s start by examining a string. Typing a string directly into the REPL causes it to be evaluated, and similar to evaluating a variable with a string in it, you get a string as the output response.

00:57 You can see what type something is in Python by passing it to the built-in type() function. Everything in Python is an object, and one way of representing an object is a class.

01:08 A class specifies how something behaves while the object is an instance of that thing. The type of my "hello" is a str class. You see that here with the words class and str for string.

01:22 Let’s try something else. 42 is a number. More specifically, it’s an instance of an int class.

01:31 9.13 is also a number, but the decimal place means it isn’t an integer. It’s an instance of the float class. You can create a new instance of a class using the class’s constructor. For strings, integers, and floats, the constructor looks a lot like a function.

01:50 Although on the surface this might look like a function because it’s got parentheses, it really is a class constructor. The str class constructor takes an argument, which is the thing you want to turn into a string.

02:02 When you pass in 42, an integer, what you get back is those digits inside of a string.

02:11 Since a str constructor returns a string object instance, it of course follows that what is returned is of type str class.

02:21 Okay, so how do you use this with input from the user?

02:27 Here, I’m going to store what the user types into a variable named age. Let me hit Enter. There’s my prompt and my answer. Let’s examine it. Variable age now contains '21'.

02:40 Pay close attention. There are quotes around it.

02:45 Yep, it’s a string. No matter what you type into an input prompt, you always get a string back. You can convert it though. Like with the str constructor, the int constructor takes an argument.

02:57 If you pass it a string that contains digit characters, it will convert the string into the corresponding integer. And there you go. The string containing two and one becomes the integer 21.

03:10 You can tell because there’s no quotes around it. Since a function returns a value, you can call that function inline with another function, passing the return from the first function as an argument to the second.

03:22 This can save some typing, and although it makes the line of code longer, sometimes it makes it clearer what you’re trying to do as it’s all in one step. Let me show you an example. Here, I’m calling the input() inside of the int constructor.

03:40 The input() function gets called first, and its response gets used as an argument to int(). Then of course, all of that gets stored in the age variable, replacing what’s already there.

03:51 Let me hit Enter. There’s my prompt. There’s me typing a response, and let’s look inside of age. The old value of age has been replaced with the new value, which is the integer 22.

04:05 Let me quickly just prove that. Yep. It’s an int. You can do the same thing with a float.

04:18 This time, the result from input() will get fed to the float constructor. Let me hit Enter. There’s the prompt. My response, it’s been a long time since I’ve been 22, or 190 pounds for that fact, but lying to the REPL is allowed.

04:35 Inside of the weight variable, you can see the response. Notice the decimal point indicating that this is a float rather than an integer, and of course, calling type() confirms it.

04:46 If you’re assigning a float to a variable, you have to include the decimal point. Otherwise, Python assumes it’s an integer even if there’s no decimal part, but since here, you’re explicitly converting to a float, it doesn’t matter. Either way, you’ll get a float, so

05:14 you get the same kind of thing if your number has a decimal place. The float constructor knows how to convert strings with decimal points inside of them.

05:23 So far, I’ve been a good little user typing in things that don’t break the code, but I don’t have to be. In the next lesson, I’ll show you what to do if your user’s a little bit naughty.

Become a Member to join the conversation.