Attention lazy Thank You Note writers! Concatenation was built for you.
This look familiar to you?
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...
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,"
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:
But wait, what about the rest of that letter?
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")
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