Summary
print(): Outputs values of any datatype to the console.- Use
endto define what follows the output. Default is a newline. - Use
septo define what separates the values.
Simple Print
You can output to the console with the function print(). You can give print() any values of any datatype as arguments and it will print it to the console.
Hello World
In this example, we give the print() function the string “Hello World”. This text will be outputted to the console.
print("Hello World") # -> Hello WorldMultiple Datatypes
You can also print any other values of any datatype using this function.
print(12) # -> 12
print(2.5) # -> 2.5
print(True) # -> True
print("Some more text") # -> Some more textBy default, after every printed value, there will be a line break. So the next value will be at the start of a new line. The code above will output the following text to the console. You can see that each output is in its own line.
12
2.5
True
Some more textWithout Any Arguments
If call the print function without any arguments, it will only output the newline.
print(1)
print()
print(2)
print()
print()
print(3)Below you see the exact output of these print statements.
1
2
3Multiple Values
The print() function can take multiple comma-separated arguments and output them. By default, these values will be separated with spaces.
print("Hello", "World", "Python") # -> Hello World PythonEnd Parameter
The parameter end defines what will be outputted after all of the printed values. You can use any string, default is a newline.
print("This doesn't end with a newline", end=" ---> ")
print("This continues", end=" ")
print("on the same line.")This doesn't end with a newline ---> This continues on the same line.Sep Parameter
The parameter sep defines how multiple printed values should be separated. You can use any string, default is a space.
print("Hello", "World", "Python", sep=" | ") # -> Hello | World | Python
print("Shopping List: Apple", "Banana", "Bread", "Milk", sep=", ") # -> Shopping List: Apple, Banana, Bread, MilkQuestions
- Explain
print()in your own words. - If the
print()function is called without any arguments, what will happen? Give and explain an example with multipleprint()functions. - Explain the parameter
endin your own words including an example. - Explain the parameter
sepin your own words including an example. - What will be the output of the following code?
print("Python", "is", "fun", sep="-", end="! ")
print("Let's code.")