Check out a free preview of the full Intermediate Python course:
The "Converting Between Types" Lesson is part of the full, Intermediate Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina introduces the split and join methods that manipulate strings based on a delimiter.

Get Unlimited Access Now

Transcript from the "Converting Between Types" Lesson

[00:00:00]
>> Nina Zakharenko: I feel like something that really encompasses this Python philosophy is how easy it is to convert between data types, and this is something I tend to use all the time. So first, we'll talk about some string operations. But let's say we have a string and it is a comma-separated string.

[00:00:29]
>> Nina Zakharenko: What data type does this look like?
>> Speaker 2: A string.
>> Nina Zakharenko: But what kind of data do we see out in the real world that looks like this?
>> Speaker 3: CSV.
>> Nina Zakharenko: Yeah, a CSV file, has everyone worked with CSV files? They're a tax for representation of data. So usually they'll have a comma-separated column line, and then all of your data is there in the file separated, just some texts with commas.

[00:01:00] So a few of you know other programming languages, how might you approach taking this CSV file and then breaking it up into something that's actually useful?
>> Speaker 4: Split?
>> Nina Zakharenko: Yeah, so in Python we can split this data,
>> Nina Zakharenko: On the comma, and we'll get back a list.
>> Nina Zakharenko: Let's say, if we had a comma-separated student.

[00:01:46]
>> Nina Zakharenko: I have Mary, she's 8, and her favorite subject is History.
>> Nina Zakharenko: Actually it's Math, Mary likes Math.
>> Nina Zakharenko: Now we can say, student.split,
>> Nina Zakharenko: On the comma, and we'll get back a list.
>> Nina Zakharenko: We saw a little bit of a tuple unpacking yesterday. I mentioned that you can also unpack variables from a list, it was just not as common.

[00:02:17] But I can go ahead and do that. So I can just say name, age, and subject is equal to student.split, right? Because student.split gave me this list. And now I have Mary's name, I have her age, I have her subject. Super easy to throw this in a loop, right, if you had to go through multiple lines.

[00:02:41]
>> Nina Zakharenko: If I had, I have my data, I'm gonna make a list out of it.
>> Nina Zakharenko: Then I'm gonna split on that comma again. So now I have my_list of individual items that were split by the comma. If I wanted to combine all the items in this list back together, the easiest and best and most efficient way to do this is with a join.

[00:03:14] So the syntax for join is a little bit weird, I see this trip up beginners. But you'll want to start with your delimiter here.
>> Nina Zakharenko: And then call join on that, and you'll parse in a sequence to join. So here, I'll use my_list, and now we'll see our new delimiter here.

[00:03:38]
>> Nina Zakharenko: If you want to make this easier to read, for example, to make it back into a CSV, I can also add a space here. And I'll easily just put a comma after each word. Notice that Python is smart and doesn't include the trailing one.
>> Nina Zakharenko: In other languages, this is a lot harder, a lot more complicated.

[00:03:59] We know that, we saw yesterday that we can parse strings into the integer.
>> Nina Zakharenko: The integer type and get back an int back.
>> Nina Zakharenko: We can make float numbers.
>> Nina Zakharenko: As you learn about looping, and comprehensions, and working with your data, you'll see exactly how powerful this is.

[00:04:29]
>> Nina Zakharenko: So open up your course website if you haven't already, click on Day 2, Intermediate Python. If you don't see it there, you might have to refresh your site. And then we're in chapter one, Exercise.