Intermediate Python Practice: Converting Between Types
Transcript from the "Practice: Converting Between Types" Lesson
>> Nina Zakharenko: Okay, let's go through our converting between types exercise.
>> Nina Zakharenko: So converting between strings and numbers, I can make a new string like this, right? Or, if I wanted to pas a number into, if I wanted to quickly convert an integer in to a string, I can pass it into the str function.
>> Nina Zakharenko: You might come across an error that looks like something like this. Let's say I wanted to print out,
>> Nina Zakharenko: Today is the, and then I wanna say, it's the 30th. So I put 30. And I'll get an error, TypeError, that says, you can only concatenate string not int, right?
[00:00:56] So if you want to concatenate these two strings, you could wrap this number in that str call and that error will go away.
>> Nina Zakharenko: Okay, so if I check the type of str, 100, we know that it's str. If I check the type, if I pass a string into int, here, we know that it would be an int.
[00:01:30] Again, if I pass the string into int, it'll come back with the integer.
>> Nina Zakharenko: We can also do this for floating point numbers.
>> Nina Zakharenko: And that will come back with the floating point number. Now, converting between lists and strings,
>> Nina Zakharenko: As I mentioned in Introduction to Python, a string is actually just kind of considered a list or a sequence of characters.
[00:02:01] So converting back and forth is pretty easy. Let's say I had a variable here called greeting and the value is hello. I can pass that greeting into a list. What will I see?
>> Nina Zakharenko: What will the type of this be?
>> Speaker 2: List.
>> Nina Zakharenko: It'll be a list. And how many items will it have in it?
>> Speaker 2: Five [INAUDIBLE].
>> Nina Zakharenko: Five items, that's right.
>> Nina Zakharenko: So easy peasy. Let's say I wanted to undo what I just did. How do I take a list of characters and smush them back into one string?
>> Speaker 2: Join.
>> Nina Zakharenko: Join, yes. There's no smush method, it's just join. Okay, so if I wanted to combine them back and separate them by commas, I could call join.
>> Nina Zakharenko: If I didn’t make a variable for this list that I just made, I can pass this whole thing into this join method.
>> Nina Zakharenko: Or I could say,
>> Nina Zakharenko: That my characters were a list of my greeting, right? So that's just kind of the same thing. If I wanted to combine all these characters back without a delimiter, I could just join on the empty string.
>> Nina Zakharenko: And that gets us back to where we started.
>> Nina Zakharenko: To split up values in a string, in a comma separated value list like you might see, we can say csv_row = "the,quick,brown,fox". And what method would we need to call to make this a list of items?
>> Nina Zakharenko: Not quite, right?
>> Speaker 2: Split.
>> Nina Zakharenko: Yeah, we'll need to call split.
>> Nina Zakharenko: So with split, we call it on the the string that we're taking action on.
>> Nina Zakharenko: But with join, what do we call the join method on?
>> Speaker 2: The string.
>> Nina Zakharenko: The string, which string?
>> Speaker 2: Delimiter.
>> Nina Zakharenko: The delimiter string, that's exactly right. So, this kinda stuff all make sense?
>> Nina Zakharenko: There's one other, well, I'll come back it, I'll cover it later, but,
>> Nina Zakharenko: I showed you yesterday how if you had a list with some duplicates in it, let's say I had a list of names.
[00:04:50] And you can type your own names here. Maybe we have Bob. We have some folks in the class. So we have a Jeff, and we have Madeline, and then we have another Nina, right? So whichever list you make just insert a duplicate. If I wanted to quickly de-duplicate this list of names, what data structure could I use?
>> Speaker 2: Set.
>> Nina Zakharenko: A set, right. A set cannot contain duplicates. So just like I was converting between the other types, I can say set, pass in my list of names, now the duplicates are gone. If I wanted to, I can convert those back into a list. If I wanted to sort it, I could pass the set into the sorted method.
[00:05:45] What comes back from sorted, what type?
>> Speaker 2: A list.
>> Nina Zakharenko: It's a list, right? Because lists have order, sets do not. Okay, so it's super easy to convert back and forth. This will be really handy as you go through your Python journey.