Joining Text
With commas, print() prints several values next to each other. Sometimes, though, you need to make one new text out of several pieces, for example to store it in a variable. For that, there is the + operator: it adds numbers and joins text. Programmers call joining text concatenation.
day = "Saturday"
message = "Today is " + day + "."
print(message)This prints:
Today is Saturday.Unlike commas, + doesn't add any spaces: it joins the texts exactly as they are. You have to add the spaces yourself, inside the quotes. That's why there is a space after is in "Today is ". Without it, you'd get Today isSaturday.
A space on its own is text too: " ". It comes in handy when you join two variables:
weekday = "Monday"
part_of_day = "morning"
print(weekday + part_of_day)
print(weekday + " " + part_of_day)This prints:
Mondaymorning
Monday morningInstructions
- 1
Create a variable
full_name: joinfirst_name, a space andsurnamewith+. - 2
Print
full_name. The output should beJohn Doe. - 3
On the next line, join
"User ",full_nameand" has logged in."with+, and print the result. The program should printUser John Doe has logged in.