1 00:00:00,780 --> 00:00:04,630 A lot of times, you'll want to represent an entire list as a string. 2 00:00:04,630 --> 00:00:08,810 Now, when we as developers see the default list representation in our shell, 3 00:00:08,810 --> 00:00:12,140 that's pretty enough for us because we understand how that works. 4 00:00:12,140 --> 00:00:14,670 But our users deserve something a little better. 5 00:00:15,800 --> 00:00:19,830 You can actually join your elements together into a single string 6 00:00:19,830 --> 00:00:23,620 using a method on strings called join and vice versa. 7 00:00:23,620 --> 00:00:25,750 There will be times when you'll have a string, 8 00:00:25,750 --> 00:00:29,840 but really it'd be more handy to have that information in a list. 9 00:00:29,840 --> 00:00:32,740 Now there's a method that is named split on strings 10 00:00:32,740 --> 00:00:36,130 that will allow us to turn a string into a list. 11 00:00:36,130 --> 00:00:39,030 Let's take a peek at how those work in the REPL. 12 00:00:39,030 --> 00:00:41,220 So let's take a look at split first. 13 00:00:41,220 --> 00:00:44,960 Now the latest Star Wars movie had a wonderful new quote in it. 14 00:00:44,960 --> 00:00:47,900 It's from the master of words himself, my man Yoda. 15 00:00:47,900 --> 00:00:52,661 He said that in his own backwards way, he said, 16 00:00:52,661 --> 00:00:56,124 the greatest teacher failure is. 17 00:01:00,021 --> 00:01:04,724 Which is totally true, I totally believe that and I think we should print this out, 18 00:01:04,724 --> 00:01:06,280 but at Yoda speed, right? 19 00:01:06,280 --> 00:01:09,820 Like one word at a time to really make it ring true. 20 00:01:09,820 --> 00:01:12,187 So, I could just build a list and 21 00:01:12,187 --> 00:01:17,738 the list could have each one of these words, but that's what split is for. 22 00:01:17,738 --> 00:01:22,764 I'm gonna say words = quote.split() and 23 00:01:22,764 --> 00:01:28,150 you'll see I now have a list of each word. 24 00:01:28,150 --> 00:01:31,120 By default, this happens, it splits on any whitespace and 25 00:01:31,120 --> 00:01:34,350 since there is a space between, it was perfect for our needs. 26 00:01:34,350 --> 00:01:37,350 You can make it split on just about anything and 27 00:01:37,350 --> 00:01:41,460 now to slow things down we're gonna need to add a little delay on our loop. 28 00:01:41,460 --> 00:01:46,390 Now lucky for us there's a module named time that provides a method called sleep. 29 00:01:46,390 --> 00:01:48,830 Let's import it and I'll show you how to use it. 30 00:01:48,830 --> 00:01:53,490 Import time and now let's just loop through each word. 31 00:01:53,490 --> 00:01:59,280 So we'll say for word in words and we'll print the word. 32 00:02:01,460 --> 00:02:05,550 And there is a method on time called sleep. 33 00:02:06,750 --> 00:02:09,660 And it takes a parameter, the number of seconds. 34 00:02:09,660 --> 00:02:12,010 So, I think Yoda's speech is pretty slow, so 35 00:02:12,010 --> 00:02:14,930 maybe one word every half second, let's do that. 36 00:02:16,370 --> 00:02:18,310 And when I press Enter here, it should go. 37 00:02:20,960 --> 00:02:24,880 The greatest teacher failure is such a wise old dude right. 38 00:02:24,880 --> 00:02:28,910 So that's split, it takes a string and turns it into a list. 39 00:02:28,910 --> 00:02:31,750 Now let's take a look at the other direction. 40 00:02:31,750 --> 00:02:33,760 Why don't we open up our meeting script. 41 00:02:36,170 --> 00:02:40,510 So, you know how calendar invites put all the people in the two-field, 42 00:02:40,510 --> 00:02:41,660 separated by commas. 43 00:02:41,660 --> 00:02:42,875 Well, let's do that. 44 00:02:42,875 --> 00:02:45,940 We've got a list and we wanna turn it into a string. 45 00:02:45,940 --> 00:02:48,030 So, let's join it together. 46 00:02:48,030 --> 00:02:51,830 Now, the first thing to do is to choose your separator. 47 00:02:51,830 --> 00:02:53,010 So let's do that. 48 00:02:53,010 --> 00:02:57,440 So we'll make a new two line and we're gonna choose our separator, 49 00:02:57,440 --> 00:03:01,220 commonly this is a comma and then a space. 50 00:03:01,220 --> 00:03:04,060 And then we're going to join that and 51 00:03:04,060 --> 00:03:07,720 we're gonna pass in our iterable, any iterable. 52 00:03:07,720 --> 00:03:10,840 And our attendees is an iterable, it's a list. 53 00:03:10,840 --> 00:03:12,820 And lists are iterable. 54 00:03:12,820 --> 00:03:16,180 And let's build the CC list, as well, the carbon copy list. 55 00:03:16,180 --> 00:03:20,754 So cc_line =, same thing, we're gonna make a comma, space, 56 00:03:20,754 --> 00:03:23,220 and we'll call join on that. 57 00:03:23,220 --> 00:03:27,310 And we'll join our optional_invitees. 58 00:03:29,210 --> 00:03:32,120 One thing to note here is that the join method 59 00:03:32,120 --> 00:03:34,450 is a method that belongs to strings. 60 00:03:34,450 --> 00:03:37,640 Now, I often go looking for this method on the list itself. 61 00:03:37,640 --> 00:03:40,380 But then I remember that since it works with iterables, 62 00:03:40,380 --> 00:03:45,270 the method belongs on the string side side of things, the greatest teacher failure is. 63 00:03:45,270 --> 00:03:47,420 So now we have a string, let's just go and print it out. 64 00:03:47,420 --> 00:03:53,080 So we'll say, print to, then we'll add the two line. 65 00:03:53,080 --> 00:03:55,238 We'll do the same thing for CC. 66 00:03:55,238 --> 00:03:59,684 So we'll say, cc, cc_line, awesome, 67 00:03:59,684 --> 00:04:04,260 let's give that a run in interactive mode. 68 00:04:04,260 --> 00:04:05,510 I'm going to drop out of here. 69 00:04:06,560 --> 00:04:12,030 We'll say Python -i and we'll call meeting.py. 70 00:04:12,030 --> 00:04:13,850 Hey, that looks beautiful, doesn't it? 71 00:04:13,850 --> 00:04:16,500 Separated by comma space. 72 00:04:16,500 --> 00:04:18,900 You know what, now that we have a string, 73 00:04:18,900 --> 00:04:21,420 we can practice bringing that back into a list, right? 74 00:04:21,420 --> 00:04:24,060 So we have two line here and 75 00:04:24,060 --> 00:04:28,970 that is a string that has a separator in there of comma and then space. 76 00:04:28,970 --> 00:04:30,590 So let's turn that it into a list. 77 00:04:30,590 --> 00:04:33,980 Now, you could imagine this on the processing side, like when you press send. 78 00:04:33,980 --> 00:04:35,530 The process gets a string, but 79 00:04:35,530 --> 00:04:38,880 it wants a list so that it can email each of these invitations. 80 00:04:38,880 --> 00:04:44,140 So let's go and do a split on this. 81 00:04:44,140 --> 00:04:47,150 So we'll say to_line.split() and 82 00:04:47,150 --> 00:04:51,830 I'm going do a comma, and a space as a parameter. 83 00:04:51,830 --> 00:04:53,720 Before we didn't have a parameter and did whitespace. 84 00:04:53,720 --> 00:04:56,130 But we want a comma and then a space to do it, right? 85 00:04:56,130 --> 00:05:00,800 So there's that, and you'll see it returned the list. 86 00:05:00,800 --> 00:05:01,510 Pretty handy, right?