Hello friends, Welcome to our website. Today we will talk about Output Through Print Statement in Python.
To understand this New topic you need to First read our last post which was on :
Output Through Print Statement
The print( ) function of Python 3.x is a way to send output to a standard output device, which normally a monitor. The simplified syntax to use print( ) function is as follows :
print(*objects, [sep =' ' or <separator-string> end = '\n' or <end- string>])
*objects eans it can be one or multiple comma-separated objects to be printed.
Let us consider some examples first :
print ("hello") # a String
print (17.5) # A Number
print (3.14159*(r*r)) # the result of a calculation, which will be performed by Python and then printed out
print ("I\'m", 12+5, "years old.") # multiple commas separated expressions
Now consider more print statement examples :
print (obj)
print (obj1, obj2, obj3)
print ('Object1 has more value than Object2')
print (obj1, 'is lesser than ', obj2)
The output of these print( ) function, you will be able to determine if the values of variables obj, obj1, obj2, obj3 are known to you. A print( ) without any value or name or expression prints a blank line.
The output of these print( ) function, you will be able to determine if the values of variables obj, obj1, obj2, obj3 are known to you. A print( ) without any value or name or expression prints a blank line.
Features of print Statement
The print statement has a number of features :
- It auto converts the items to strings i.e., if you are printing a numeric value, it will automatically convert it into an equivalent string and print it; for numeric expressions, it first evaluates them and then converts the result to a string, before printing.
- It inserts spaces between items automatically because the default value of sep argument is space(' '). The sep argument specifies the separator character. The print( ) automatically adds the sep character between the items/objects being printed in a line. If you do not give any value for sep, then by default the print( ) will add a space in between the items when printing. Consider this code :
will print
My name is Amit.
You can change the value of separator character with sep argument of print( ) as per this :
The code :
print ( "My", "name", "is", "Amit.", sep = '...')
will print
My...name...is...Amit.
- It appends a newline character at the end of the line unless you give your own end argument. Consider the code given below :
print (" I am 16 yrs old")
It will produce output as :
My name is Amit.
I am 16 yrs old
So, a print ( ) statement appended a newline at the end of objects it printed, i.e., in above code :
My name is Amit. ↲ or \n
I am 16 yrs old
The print( )works this way only when you have not specified any end argument with it because by default print( ) takes value for the end argument as '\n' - the newline character.
If you explicitly give an end argument with a print( ) function then the print( ) will print the line and end with the string specified with end argument, e.g., the code
print (" my name is Amit.",end = '$')
print ("I am 16 years old.")
will print output as :
my name is Amit. $I am 16 years old.
So the end arguments determine the end character that will be printed at the end of the print line.
So with this, we end with this Topic, don't forget to follow me so that you can get the latest updates of my new posts that I upload weekly, I will try my best to upload daily OR you can Sign up for Newsletters at the top of the Blog and that will give you E-mail updates of my New Post.
Jai Hind!!!!
Vande Mataram!!!
If you explicitly give an end argument with a print( ) function then the print( ) will print the line and end with the string specified with end argument, e.g., the code
print (" my name is Amit.",end = '$')
print ("I am 16 years old.")
will print output as :
my name is Amit. $I am 16 years old.
So the end arguments determine the end character that will be printed at the end of the print line.
So with this, we end with this Topic, don't forget to follow me so that you can get the latest updates of my new posts that I upload weekly, I will try my best to upload daily OR you can Sign up for Newsletters at the top of the Blog and that will give you E-mail updates of my New Post.
Jai Hind!!!!
Vande Mataram!!!
No comments:
Post a Comment
If you have any doubts, please do let me Know.