The print function is arguably the most used function in Python. It shows the user vital information about the code on the screen as an output. We have seen how it works in Section1.1 when we printed "Hello, World!".
There are many methods of using print. We will introduce 3 in this section.
Syntax
print()
We need to pass a parameter/argument* to the function by typing it in the parentheses ( ) . Here, the parameter/argument is a string data "Hello, World!". More about strings in Section 1.4.
print("Hello, World!")
*Parameter vs Argument
The terms parameter and argument are usually used interchangeably to refer to information passed to the function.
Method 1 - Printing a data value
In this method, we are passing a data value - "Hello, World!" - directly to the function.
Method 2 - Assign data to a variable
In this method, we first assign "Hello, World!" into a variable called message. We then pass the variable into the print function.
Method 3 - Concatenate
This method uses concatenation, which means putting strings together. We use the + operator, and we can do this with as many + separating different strings within the print function.
Note that when we use + to combine the strings together, you may need to insert a space in between the strings.
Watch from 1:30 to 2:20
You are expected to comply with University policies and guidelines namely, Appropriate Use of Information Resources Policy, IT Usage Policy and Social Media Policy. Users will be personally liable for any infringement of Copyright and Licensing laws. Unless otherwise stated, all guide content is licensed by CC BY-NC 4.0.