HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

8) Text and Strings Lesson

Python String Concatenation

3 min to complete · By Martin Breuss

In the previous lesson, you learned that a string in Python is a collection of characters. As an example of how Python thinks of a word, you saw the word "hello" stuck together by all the different characters that make it up:

"h" + "e" + "l" + "l" + "o"

While this might look a little unfamiliar, it is actually valid Python code. Head over to your Python console and type in the exact sequence of characters, and press Enter:

Concatenating the characters of the word hello individually in a Python console

Define Concatenation

Python takes all the single characters and combines them into one string: the word "hello". What you did here is called string concatenation. As you can see, it really is just a fancy word for saying that you are adding characters together.

Colorful illustration of a light bulb

Info: You might have noticed that you are using the same plus (+) operator that you used for adding numbers. Python operators have different effects on different data types.

This doesn't only work with single characters but with strings of any length. You can always use the plus (+) operator to stick them together:

job = "coding"
location = "nomad"
print(job + location)

Feel free to experiment with adding other strings together.

Practice Concatenation in Python

Create a sentence by concatenating the words "this", "is", and "fun".

Answer these Questions

Try answering the following questions, and feel free to post your thoughts and your solution on Discord:

  • What happens when you put the words together without any further changes?
  • What do you need to add in order to create a readable sentence? Why?
  • Challenge: Can you keep the same words and create a question by concatenating them differently? Remember to add spaces and punctuation.

What happens when you use a string that consists of more than one word? What do you need to do in order to create a sentence with normal spacing that makes it readable?

Summary: Python String Concatenation

  • Concatenation is the combination of characters or strings to create a single string
  • When you use the plus operator (+) on strings, Python performs concatenation
  • String concatenation works with strings of all lengths