Concatenation explained
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...
- 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!
Make sure to open a Trinket file to follow-along! (How to create a Trinket file?)
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")
If you're asking yourself what's up with the empty print statements, refer to print() Function | Python in 30 Minutes.
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
Practice problem
In a new Trinket file, create a Python program that asks the user for an input by asking "What is your favorite song?". You should save their response as a variable. After they type it and press enter, their song should be printed like "Insert Song Name is your favorite song." in the output.
Check out this slideshow to see what your output should look like:
HINT #1: Don't remember how to ask the user for an input? Check the input() function template on the last page of your Python Cheat Sheet for some help!
HINT #2: Don't remember how to print content onto the webpage? Check the print() function template on the last page of your Python Cheat Sheet for some help!
Next up, we'll learn how to use conditionals (if, elif, else) to run code dependent on a set of conditions being met. Click below to continue!
Python Cram Kit
Want to unlock (PREVIEW ONLY) content? Get your Python Cram Kit now!
![]() | Apply | PRACTICE PYTHON EXAM (PREVIEW ONLY) |
![]() | Tools | Python Nerd Notes (PREVIEW ONLY) |
![]() | Concept | Print function![]() |
![]() | Concept | Input function![]() |
![]() | Concept | Concatenation![]() |
![]() | Concept | Conditionals (if, elif, else)![]() |
![]() | Concept | While loops (PREVIEW ONLY) |
![]() | Concept | For loops (PREVIEW ONLY) |
![]() | Concept | Data Types (PREVIEW ONLY) |
![]() | Concept | Nested Conditionals (PREVIEW ONLY) |
![]() | Concept | Flag Variables (PREVIEW ONLY) |
