Output Variables
To show the value of a variable, you will usually reach for the print() function.
Print a variable
Prompt
Create a variable x with value "Python is awesome" and print it.
Output: complete Python program
main.py
x = "Python is awesome"
print(x)
Comma separated
Separate several variables with commas to print them all with one print() call:
Prompt
Create three variables x="Python", y="is", z="awesome" and print them separated by commas in one print call.
Output: complete Python program
main.py
x = "Python"
y = "is"
z = "awesome"
print(x, y, z)
Plus operator
The + operator can join several variables for output as well:
Prompt
Create three variables x="Python ", y="is ", z="awesome" (with trailing spaces) and print them concatenated with the + operator.
Output: complete Python program
main.py
x = "Python "
y = "is "
z = "awesome"
print(x + y + z)
Number addition
Between numbers, + does what it does in math and adds them:
Prompt
Create two number variables x=5 and y=10, then print their sum using the + operator.
Output: complete Python program
main.py
x = 5
y = 10
print(x + y)
String and number error
Mixing a string and a number with + doesn't work, and Python responds with an error:
Prompt
Demonstrate a TypeError by trying to combine a number variable (x=5) and a string variable (y="John") with the + operator in print.
Output: complete Python program
main.py
x = 5
y = "John"
print(x + y)
Mixed types with comma
For printing several variables, commas are the best choice, because they also handle a mix of data types:
Prompt
Print a number variable (x=5) and a string variable (y="John") separated by a comma in the print function.
Output: complete Python program
main.py
x = 5
y = "John"
print(x, y)