fbpx

Concatenation

Concatenation explained

Attention lazy Thank You Note writers! Concatenation was built for you.

This look familiar to you?

Template for the typical thank you note

Remember cranking out a note every 10 seconds with this strategy?

You didn't know it, but as a kid you were implementing concatenation here!

Concatenation is combining variables with strings to create one complete message.

In relation to the Thank You Note...

  • the variable was the person's name we were addressing the Thank You Note to.
  • the strings were the rest of the template that we would just copy and paste onto each note.

Coding with concatenation

Let's build our own Thank You Note template: Python edition!

To start, let's ask the user who they're writing the letter to with an input function...

addressed_to = input("Who are you writing this Thank You Note to?")

...and save that value to a variable called addressed_to.

addressed_to = input("Who are you writing this Thank You Note to?")

Now, let's focus on structuring the greeting of our Thank You Note in a string.

Dear _____,

In Python terms, this would look like so:

"Dear _____,"

But… we don't want to actually write "_____" in our Thank You Note. Instead of that, we want to replace it with the value of addressed_to.

To do so, we'll concatenate the addressed_to value into the string like so:

"Dear " + addressed_to + ","

A few things to take note of here... (for the sake of understanding, let's make addressed_to equal "David")

Question: What's the purpose of the "+" on each side of addressed_to?
Answer: This is how we concatenate the value of addressed_to into the string.

Put in other terms...

Think of the "+" signs like glue. Imagine you're breaking the string in half and linking it back together with the variable. The "+" signs act as the glue between the broken string and the variable.

Question: Why does the word "Dear" have a space after it?
Answer: Because if it didn't, our concatenated value would equal "DearDavid," instead of "Dear David,".

Remember, Python is dumb and needs you to declare each and every necessary space.

Question: Why is "," at the end?
Answer: If we didn't have it, our concatenated value would equal "Dear David" instead of "Dear David,"

Let's scratch the above Q&A and walk through this step-by-step.

Consider a situation where the user types "abc" into the prompt for the addressed_to value.

This would result in the following output, since we're addressing the letter to "abc".

Dear abc,

Now, let's look at this from Python's perspective to see what's really going on.

When the user enters "abc" as the addressed_to value, instead of seeing the following…

"Dear " + addressedTo + ","

…Python actually sees this:

"Dear " + "abc" + ","

Now, let's glue together the first two strings.

"Dear abc" + ","

Perfect, we've glued together "Dear " and "abc" to equal "Dear abc".

Now, let's glue the "," onto "Dear abc".

"Dear abc,"

Awesome! We've successfully concatenated the addressed_to value of "abc" into our Thank You Note greeting!

Alright, now that we've figured out how to concatenate our greeting, we'll place it in a print function.

addressed_to = input("Who are you writing this Thank You Note to?")
print("Dear " + addressed_to + ",")

Type the above code into your Trinket file. It should look like this now:

Now run your program by clicking the "Run" button above your code. Check out what your output should look like in this slideshow:

  • The input question appears, asking the user who they're writing to.

But wait, what about the rest of that letter?

Template for the typical thank you note

If we wanted to include the rest of the letter, we can include more print functions below our first one we wrote above.

addressed_to = input("Who are you writing this Thank You Note to?")
print("Dear " + addressed_to + ",")
print
print("Thank you so much for the birthday gift! I greatly appreciate it!")
print
print("From,")
print("Adam")

Revisiting the concatenation template

Now that we've learned how concatenation works behind-the-scenes, here's a template for it that you can reference later:

string + variable + string

Keep in mind, we could place the variable at the beginning...

variable + string

...or end...

string + variable

Additionally, it's important to remember that if your variable is an integer, you need to surround it with the str function like so...

string + str(integer) + string

..and if it's a float, do the same.

string + str(float) + string


Concatenation – Practice!