A message can be displayed on the screen using the print function.
Syntax:
print(object, separator, end, file, flush)
Object is the information we want to display on the screen.
Separator is used to show separation.
End refers to what should be given at the end.
Program-1
Let's see how to display vijanthi tutorial message on screen using print function.
print("vijanthi tutorial")
print('vijanthi tutorial')
Output:
vijanthi tutorial
vijanthi tutorial
Description:
In the first print function, the message vijanthi tutorial given inside the double quotes is displayed.
Similarly, the print function in the second line displays the message vijanthi tutorial given inside the single quotes.
If you want to display a message using the print function, you must enclose it in single quotes (‘) or double quotes (“).
Program-2
Let's see vijanthi tutorial's how to display this line with single quotes.
print("vijanthi tutorial's")
output:
vijanthi tutorial's
Description:
In the sentence vijanthi tutorial's, the word tutorial's is given in single quotes. If you want to display this sentence, you have to put the full sentence inside the double quotes.
In the print function, all words between where the double quotes start and end are displayed.
Program-3
Let's see how to display a sentence with double quotes using the Print function.
print('"vijanthi tutorial"')
Output:
"vijanthi tutorial"
Description:
The full sentence "vijanthi tutorial" is given in double quotes. If you want to display this sentence with double quotes, you have to put this sentence inside single quotes in the print function.
Note:
If you want to print a word with single quotes, you have to put it inside double quotes in Print function.
If you want to print a word with double quotes, you have to put it inside single quotes in Print function.
Program-4
Let's see how to print a message with double quotes and single quotes.
print('"you're here to learn python"')
Output:
"you're here to learn python"
Description:
Since we are going to print a sentence along with double quotes, first we put this sentence inside single quotes.
After that there is a single quotes word inside the sentence. A backslash (\) should be given before the single quotes to print it along with the single quotes.
The reason for giving the backslash is to treat the single quotes as a string.
Program-5
print("welcome\nto\ntutorial")
Output:
welcome
to
tutorial
Description:
“\n” is new line.
First the print() function prints the word welcome. The following word “\n” moves the cursor to the next line.
The word to is printed on that line. A “\n” next takes the cursor to the next line. Now the word tutorial is printed on the new line moved.
Using “\n” in print() function can print one sentence in multiple sentence.
Program-6
print("name\t:\tvijanthi tutorial")
Output:
name : vijanthi tutorial
Description:
“\t” is tab.
tab gives a spacing of 4 spaces.
There is a bit too much space between name and colon(:). Also there is a bit too much gap between colon and vijanthi tutorial.
Why more space is given then “\t” is given in that place.
Program-7
print("vijanthi","tutorial",sep="-")
output:
vijanthi-tutorial
Description:
The “-” symbol given in sep is placed between two words. sep is used to separate two words.
The default value of sep is blank space.
Program-8
print("vijanthi",end=" ")
print("tutorial",end=".")
output:
vijanthi tutorial.
Description:
The default value of End is “\n”.
Two print statements are given here. So we expected the two words vijanthi and tutorial to be printed separately. But here it is printed as a single sentence.
The reason for this is the end parameter in the print function. First look at the print function. The word vijanthi is printed in it. Only blank space is given in the end after that. This space is the reason for bringing up the word tutorial below.
The word tutorial in the second print function is printed. Dot is given in the next parameter called end. That is why dot is given after the word tutorial.