Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

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 morning

Instructions

  1. 1

    Create a variable full_name: join first_name, a space and surname with +.

  2. 2

    Print full_name. The output should be John Doe.

  3. 3

    On the next line, join "User ", full_name and " has logged in." with +, and print the result. The program should print User John Doe has logged in.

Back

8/15

  1. 1. Printing
  2. 2. Arithmetic Operations
  3. 3. Quiz: Order of Operations
  4. 4. Comments
  5. 5. Variables
  6. 6. Changing a Variable's Value
  7. 7. Division and Remainder
  8. 8. Joining Text
  9. 9. Text and Numbers
  10. 10. F-strings
  11. 11. Reading Errors
  12. 12. Quiz: Spot the Mistake
  13. 13. Quiz: Changing a Variable
  14. 14. Quiz: The Order of Commands
  15. 15. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.