Skip to content

More on Strings

  • Lesson
  • 1 min read
  • 4 of 6 in this chapter

Concatenation

Another thing we should cover before getting to the logic part of programming is concatenation.

Concatenation is an intuitive way to combine strings. All you have to do is put a + between strings to combine them. It is especially useful in making results understandable.

Python
num1 = 6
num2 = 15
sum = num1 + num2
print("The sum is " + str(sum))

OUTPUT

Text
The sum is 21

Do note that the sum variable is storing an integer value.

When we use the + in the print statement, python doesn't know whether to add two numbers together or two pieces of text together.

"The sum is" is a string while sum is an integer.

Luckily, in Python we can put the variable inside str() to convert it from a number to a string and confirm that we just want to add two strings together. Thus, the output is as we want it, a string.

If we do not do this, an error will show up.