Concatenation | BUS 104 – Python Exam

Still feeling nervous for your exam? Check out the Python Cram Kit for practice questions that emulate the difficulty that you'll face on your exam. Each question includes a thorough step-by-step explanation where I walk you through the entire problem solving process!

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!

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,"

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("Thank you so much for the birthday gift! I greatly appreciate it!")

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:

  • The user is asked for their favorite song.

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!

favorite_song = input("What is your favorite song?")
print(favorite_song + " is your favorite song.")

If you named your variable favorite_song something else, that's okay. As long as you're output was the same, you're good to go!

Python Follow-along Guide

It's no secret you retain info better when you write it down. That's why I created the Python Follow-along Guide for you!

As you come upon key concepts highlighted in yellow, like this sentence here, you can fill-in-the-blanks on your Python Follow-along Guide so that you remember all the important stuff for later!

You can obtain your Python Follow-along Guide for FREE by entering your email below!

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!


Each exam concept broken down with relatable situations to your life!

ToolsPython Follow-along Guide (FREE)
LessonPrint function
LessonInput function
LessonConditionals (if, elif, else)
LessonWhile loops
LessonFor loops
LessonData Types
LessonNested Conditionals
LessonFlag Variables

Practice Problems

Include step-by-step explanations through each part of the problem!

Python Cram Kit Logo

"...I was blown away with the videos and the ability to explain what is going on at a simple level. Highly recommend!"

Jack Whelan
March 8, 2021

Rating: 5 out of 5.

Leave a Comment