Let's take a deep dive into how we represent text in Python.

Basics

If you recall from the previous section, we can create a string with the " and ' surrounding the text:

a = "hello"
print(a)   # hello

But what if we wanted to do this?

name = "John"
message = "Hello"

print(message)
print(name)

# Hello
# John

You'll notice that we get two separate lines! That's not great, because I want to say "Hello John" instead. Here, we get our first lesson about print: it prints the text on a new line. So how would we add the strings together?

Concatenation

We can concatenate two strings together to add them, with the + operator, like this:

name = "John"
message = "Hello"
print(message + name)
# HelloJohn

As you can see, it literally adds them together, no extra space in the middle or anything. If we're going to concatenate things, they both have to be strings. We can't concatenate a string and an integer, for example.

a = 1
b = "2" # this is a STRING, not an INTEGER
a + b

# TypeError: unsupported operand type(s) for +: 'int' and 'str'

However, we can convert anything to a string using the str() function. Here's what that looks like:

a = 1
b = "2" # this is a STRING, not an INTEGER
str(a) + b
# "12" (which is a string, not an integer)