Split and Join5:01 with Craig Dennis
Strings provide excellent ways to create lists as well as to create a string representation of your lists. Let's explore `str.split` and `str.join`!
A lot of times, you'll want to represent an entire list as a string. 0:00 Now, when we as developers see the default list representation in our shell, 0:04 that's pretty enough for us because we understand how that works. 0:08 But our users deserve something a little better. 0:12 You can actually join your elements together into a single string 0:15 using a method on strings called join and vice versa. 0:19 There will be times when you'll have a string, 0:23 but really it'd be more handy to have that information in a list. 0:25 Now there's a method that is named split on strings 0:29 that will allow us to turn a string into a list. 0:32 Let's take a peek at how those work in the REPL. 0:36 So let's take a look at split first. 0:39 Now the latest Star Wars movie had a wonderful new quote in it. 0:41 It's from the master of words himself, my man Yoda. 0:44 He said that in his own backwards way, he said, 0:47 the greatest teacher failure is. 0:52 Which is totally true, I totally believe that and I think we should print this out, 1:00 but at Yoda speed, right? 1:04 Like one word at a time to really make it ring true. 1:06 So, I could just build a list and 1:09 the list could have each one of these words, but that's what split is for. 1:12 I'm gonna say words = quote.split() and 1:17 you'll see I now have a list of each word. 1:22 By default, this happens, it splits on any whitespace and 1:28 since there is a space between, it was perfect for our needs. 1:31 You can make it split on just about anything and 1:34 now to slow things down we're gonna need to add a little delay on our loop. 1:37 Now lucky for us there's a module named time that provides a method called sleep. 1:41 Let's import it and I'll show you how to use it. 1:46 Import time and now let's just loop through each word. 1:48 So we'll say for word in words and we'll print the word. 1:53 And there is a method on time called sleep. 2:01 And it takes a parameter, the number of seconds. 2:06 So, I think Yoda's speech is pretty slow, so 2:09 maybe one word every half second, let's do that. 2:12 And when I press Enter here, it should go. 2:16 The greatest teacher failure is such a wise old dude right. 2:20 So that's split, it takes a string and turns it into a list. 2:24 Now let's take a look at the other direction. 2:28 Why don't we open up our meeting script. 2:31 So, you know how calendar invites put all the people in the two-field, 2:36 separated by commas. 2:40 Well, let's do that. 2:41 We've got a list and we wanna turn it into a string. 2:42 So, let's join it together. 2:45 Now, the first thing to do is to choose your separator. 2:48 So let's do that. 2:51 So we'll make a new two line and we're gonna choose our separator, 2:53 commonly this is a comma and then a space. 2:57 And then we're going to join that and 3:01 we're gonna pass in our iterable, any iterable. 3:04 And our attendees is an iterable, it's a list. 3:07 And lists are iterable. 3:10 And let's build the CC list, as well, the carbon copy list. 3:12 So cc_line =, same thing, we're gonna make a comma, space, 3:16 and we'll call join on that. 3:20 And we'll join our optional_invitees. 3:23 One thing to note here is that the joint method 3:29 is a method that belongs to strings. 3:32 Now, I often go looking for this method on the list itself. 3:34 But then I remember that since it works with iterables, 3:37 the method belongs on the string side effect, the greatest teacher failure is. 3:40 So now we have a string, let's just go and print it out. 3:45 So we'll say, print to, then we'll add the two line. 3:47 We'll do the same thing for CC. 3:53 So we'll say, cc, cc_line, awesome, 3:55 let's give that a run in interactive mode. 3:59 I'm going to drop out of here. 4:04 We'll say Python -i and we'll call meeting.py. 4:06 Hey, that looks beautiful, doesn't it? 4:12 Separated by comma space. 4:13 You know what, now that we have a string, 4:16 we can practice bringing that back into a list, right? 4:18 So we have two line here and 4:21 that is a string that has a separator in there of comma and then space. 4:24 So let's turn that it into a list. 4:28 Now, you could imagine this on the processing side, like when you press send. 4:30 The process gets a string, but 4:33 it wants a list so that it can email each of these invitations. 4:35 So let's go and do a split on this. 4:38 So we'll say to_line_split and 4:44 I'm going do a comma, and a space as a parameter. 4:47 Before we didn't have a parameter and did whitespace. 4:51 But we want a comma and then a space to do it, right? 4:53 So there's that, and you'll see it returned the list. 4:56 Pretty handy, right? 5:00
You need to sign up for Treehouse in order to download course files.Sign up