Concatenation | BUS 104 – Python Exam

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!

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!

ToolsPython Nerd Notes (PREVIEW ONLY)
ConceptPrint function
ConceptInput function
ConceptConditionals (if, elif, else)
ConceptWhile loops (PREVIEW ONLY)
ConceptFor loops (PREVIEW ONLY)
ConceptData Types (PREVIEW ONLY)
ConceptNested Conditionals (PREVIEW ONLY)
ConceptFlag Variables (PREVIEW ONLY)

Leave a Comment