String Concatenation (Solution)
00:00 The task was to create two strings and assign them to variables,
00:10 concatenate them without a space, and concatenate them with a space.
00:22
Let’s get started by creating two variables that point to strings. I’ll start with string_left
, which I will point to a string called "bat"
, and then I’ll do another one, string_right
, that I’ll point to "man"
.
00:44
And now I can use a call to print()
to stick these two together. So I’ll open up the parentheses after writing print
, and then type string_left
00:55
plus string_right
. Close the parentheses. So as you can see here, I’m using the plus operator (+
), which if you use it with strings, it concatenates them together, so it’ll literally stick bat
and man
together without any space in between.
01:12
I press Enter to confirm we get the string batman
stuck together. Now the last task was to concatenate them with a space. So I will just copy my call to print()
and jump into the middle of this, next to the plus operator.
01:28 And here I’ll add yet another string, just a whitespace character.
01:35
So I’m opening up the quotes, putting a whitespace character, and then closing the quotes. Again, that’s a perfectly valid string. It’s just a string that only consists of one character, which is a space (" "
) in that case. Again, I’m using plus operators left and right to stick these three strings together.
01:54
So now when I press Enter, I will get one string, bat man
, all in one. And that’s the exercise.
Become a Member to join the conversation.