Lesson 11 +10 XP

Output Variables

Output Variables

Printing variables is how you see what your program is doing.

Print a single variable

x = "Python is awesome"
print(x)  # Python is awesome

Print several variables

Use commas to print multiple values with spaces:

x = "Python"
y = "is"
z = "awesome"
print(x, y, z)  # Python is awesome

Combine strings with +

For strings you can also use +:

print(x + y + z)  # Pythonisawesome

Notice there is no space this time!

Numbers and + or comma

With numbers, + means addition:

a = 5
b = 10
print(a + b)  # 15

But mixing a string and a number with + fails:

print(x + a)  # TypeError

The comma version always works:

print(x, a)  # Python 5

TL;DR

  • print(x) prints one variable.
  • Commas add spaces and mix any types.
  • + glues strings but adds numbers.
  • Never mix string and number with +.