Transcript from the "Strings" Lesson
[00:00:36] Anybody know what we call these tiny little units of text that usually correspond to something like a letter? Any idea what we call these? So we call these little units or these little letter beads.
>> Characters, thank you Chat, beautiful. So characters are what strings are made of.
[00:00:57] Each string is made up of zero or more character units. And we could think of these as the little beads that we could string together to spell out a word or a whole sentence or whatever else. Maybe it's just emoji, who knows? So characters are the kind of individual units that make up a string.
[00:01:23] So earlier when we had the .length that we saw returned, we saw type of some string .length as a number. The thing that that number to is the number of characters in the string. So in this case, for example, maybe the second example is easier to find. So what do you think super in quotes .length is going to return?
>> Five, cuz we can count that. Let's just confirm. Okay, now, how about supercalifragilisticexpialidocious .length? Does anybody off the top of their heads know how to count a string that long? No, not me. Let's find out, how long is this super long string? I didn't copy.
[00:02:11] Let's find out. Okay, it's not working. Let's type it out, fragilisticexpialidocious. 34 characters that's too long, it's too many characters that's why I couldn't even handle it [LAUGH]. [LAUGH] So when we wanna find out how many characters we're working with in a particular string, we can use the .length property to tell us.
[00:02:40] And, well, just another note, let's say we had a string like before. I had some string .length. If I type that in, I get 11. So some four letters, string as five letter six letters. Well, where's that extra letter coming from?
>> The space. The space itself is a character.
[00:03:03] So I can even have a string of just a space, and that is still a valid character. Space is as much a thing as a letter is. We can also have a string with no characters, which is called the empty string. And if I asked for the length of that it will tell me 0, still a string.
[00:03:26] Even though it has nothing inside of it, it's still a value of typestring which I can prove to myself with typeof. So, characters are the units from which strings are made up. And in each string, characters are in a specific order, right? It would be kind of chaos if they weren't, if every word or sentence that we tried to type was just like whatever order you want, a jumble of beads.
[00:03:54] When we string together these beads, these little characters we want them to go in a specific order, and so each character gets a number. So, if we have our string ALOHA, Like on that little letter bead. We're imagining this on like a little beaded bracelet or something like that.
[00:05:15] And 1 at being the point, one character later, and so on and so forth. So 2, 3, 4 and then at the end of the string would come a five, and if we ask this, hey, how long is this string? It'll say ALOHA has five characters, it's five characters long.
[00:05:40] And if we asked it, what is the character at position one? Let's say, it would tell me which character starts at that number, at that one. So in this case, that would be L, exactly. So these are two different ways that you can think about the numbers assigned to each character.
[00:06:04] One is that each kind of chunk gets its own number. And the other is that the numbers actually kind of appear in between the chunks in between the characters. And they are an indicator of which character starts there, if that makes sense. Later on in life, we might find that having that second mental representation is helpful when we're talking about substrings or smaller strings within the string.
[00:06:41] But for now, either way, whichever way you find more useful to think about it, both of them are valid.