DEV Community

Cover image for THE PRINT() FUNCTION
Pixel
Pixel

Posted on

THE PRINT() FUNCTION

Image description

By the end of this lesson you should be able to:

  1. Describe the print function and its usage as used in python.
  2. Write a simple code using the print function.
  3. Avoid common errors associated with the print function.

Let's hop right into the action!

The print function

-The print statement is one of the easiest ways to display data such as texts to the user using python.
To bring it into perspective, adding a print function tells the computer, 'Display whatever's in the brackets on the user's console'.
-Here's a good example of a print function

print("Hello world!")
Enter fullscreen mode Exit fullscreen mode

As illustrated above, the print function is made up of three components:Print, Parentheses() and Double quotation marks""
-A point to note is that all of the above mentioned components must be included or the code won't work! This is further illustrated in the common errors associated with the print function below.

-The double quotations can also be replaced by single quotation marks and the code would still work.

print('Hey, I love your smile!')
Enter fullscreen mode Exit fullscreen mode

Common errors

Here are examples of errors that most beginners commit and simple solutions to the errors.

Error 1

Print("I love travelling")
Enter fullscreen mode Exit fullscreen mode

-The statement print above has been capitalized and the code will therefore not work.The print statement must therefore be written in small letters for the code to work.
-The code will also not work if the print statement is misspelled.
-The correct code should be:

print("I love travelling")
Enter fullscreen mode Exit fullscreen mode

Error 2

print(John was sent to the shopping center to buy meat.)
Enter fullscreen mode Exit fullscreen mode

-The code above will not work because the quotation marks "" or ''
have not been included in the code.Instead, the code should be written as:

print("John was sent to the shopping center to buy meat.")
Enter fullscreen mode Exit fullscreen mode

Error 3

print"Chicken wings are the best!"
Enter fullscreen mode Exit fullscreen mode

-The code does not have parentheses() and will therefore not work. The code should be re-written as:

print("Chicken wings are the best!")
Enter fullscreen mode Exit fullscreen mode

Error 4

print("I am Darkside be prepared to meet
                          your
                           doom!")
Enter fullscreen mode Exit fullscreen mode

-The code above if run fails to display the message on the user's console.But why? Haven't we included all the required components?
-The problem can be easily solved by using triple quotes """text """ instead of double quotes ""text""

print("""I am Darkside be prepared to meet
                          your
                           doom!""")
Enter fullscreen mode Exit fullscreen mode

-Please note that double quotes are only used for single line texts.
-Triple quotes""" are used for multi-line texts

Please leave any question that you may be having in the comments section.That's all for today.
Thank you!

Top comments (0)