4.3 Output Variables

By | September 29, 2021

The Python print statement is often used to output variables.

To combine both text and a variable, Python uses the + character:

Example

x = “awesome”
print(“Python is ” + x)

Output:
Python is awesome

You can also use the + character to add a variable to another variable:

Example

x = “Python is “
y = “awesome”
z =  x + y
print(z)

Output:
Python is awesome

For numbers, the + character works as a mathematical operator:

Example

x = 5
y = 10
print(x + y)

Output:
15

If you try to combine a string and a number, Python will give you an error:

Example

x = 5
y = “John”
print(x + y)

TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’


Leave a Reply

Your email address will not be published. Required fields are marked *