Python Basics – print() function
(Interested in learning more about Python? Learn Python in 3 weeks, under 10 minutes each day today by clicking here.)
Notes from the video ‘Python Course Singapore | Learn Python Programming |Python Tutorial 1 – Python print()‘:
In this video, we would try to print the string “This is an example“.
But… before that, let’s break down this code down into smaller parts.
The print() function
First let’s look at the print() function.
The print() function is a function that displays whatever is inside the parentheses.
print is the name of the function.
Notice that print is in lowercase. If we were to capitalize it, the program won’t run.
This is true for all function names. So, ensure that whenever you type in the name of a function, it’s in lowercase.
Beside the name of the function, ‘print’, you will need to include the parentheses. The parentheses tells Python to “call” the print function, which is basically telling Python to execute the function. And In the case of the print function, it’s to display whatever is inside the parentheses.
The string “This is an example”
In the example, we included the quoted text “This is an example” in the parentheses. This quoted text is called a string. Like the name suggests, a string is a string or sequence of characters.
The double quotes tell Python that it’s dealing with a string. The double quotes do not get displayed when python displays the string.
For example, when you use the print function to print the string, “This is an example.” The console only displays the text This is an example without the double quotes.
Single Quotes and Double Quotes in Python
Instead of double quotes, you can also single quotes to enclose the string.
But you have to make sure that you are consistent when you use the quotes.
For example, if you start a string with a single quote, you have to end it with a single quote and if you start a string with double quotes, you should end it with double quotes.
Otherwise you would get a syntax error.
In general, there’s not much difference between using single or double quotes for strings. The only time it matters is when the single or double quotes are part of the string.
Let’s look at some examples.
If the text in the string is, He’s here. You shouldn’t just enclose the string with single quotes otherwise you will get a syntax error.
There are a few ways you can go around this problem:
First, you can change the single quotes to double quotes.
Secondly, you can keep the single quotes and include a backslash or escape character (\) between ‘e’ and the single quote.
Let’s look at another example.
If the text in the string is, She said “hi”. You shouldn’t just enclose the string with double quotes otherwise you will get a syntax error.
Again, there are a few ways you can go around this problem:
Firstly, you can change the double quotes to single quotes.
Secondly, you can keep the double quotes and include a backslash or escape character (\) before each double quotes in the text.
If you follow either of these steps, you will be able to print the string successfully.