Input And Output In Python – print() – Free Python Tutorials

The main job of most of the Programming Language is:

  • Taking some input (may be from a user or from other sources).
  • Processing the input accordingly.
  • Generate and present the output.

There is no violation of the above-mentioned points in Python also. To take input from the user, there is a function ‘input()’ and to generate output, there is another function ‘print()’. In this tutorial, we will discuss these two functions in brief.


Printing output in Python

To print generated output or to print anything to the screen we use print() function in Python. The general prototype of the print() function is:

print(“<Your Message will be here>”) or print(‘<Your Message will be here>’)

Now we can use print() function in many ways:

  • If we pass any string object in print() function as an argument, then that string object will be printed. As example:
    # String using double quotes(" ")
    
    >>> print("Rajesh Sinha")
    Rajesh Sinha
    
    # String using single quotes(' ')
    
    >>> print('padhle.com')
    padhle.com

     

  • If we pass any object other than of string type as an argument in print() function, then we don’t need to give any type of quotations. But if we are passing any string object then we have to give quotations, otherwise we will get an error. As example:
    # Printing integer value
    
    >>> print(5)
    5
    
    # Printing integer value
    
    >>> print(5.0)
    5.0
    
    # Printing boolean value
    
    >>> print(True)
    True
    
    # Printing boolean value
    >>> print(False)
    False
    
    # Printing string value without quotes
    
    >>> print(Rajesh)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'Rajesh' is not defined

    So we can conclude that if we are printing string objects then only we have to provide quotes, otherwise not.

  • If we want to print the value of any variable then we have to just pass that variable as an argument in print() function without any quotes. As example:
    # Storing integer value
    
    >>> a=5
    
    # Storing float value
    
    >>> b=5.0
    
    # Storing boolean value
    
    >>> c=True
    
    # Storing string value
    
    >>> d="Rajesh"
    
    # Printing values of the variables
    
    >>> print(a)
    5
    >>> print(b)
    5.0
    >>> print(c)
    True
    >>> print(d)
    Rajesh

    Note that, when we are passing the variable name as an argument in print() function to print its value, the variable name should not be within any quotes. If we pass the variable name within quotes, the instead of the value of the variable, the variable name will be printed. As example:

    # Storing integer value in a
    
    >>> a=5
    
    # This is the correct way to print the value of a variable
    
    >>> print(a)
    5
    
    # But these two are wrong and we won't get our desired output
    
    >>> print("a")
    a
    
    >>> print('a')
    a

     

  • If we want to add multiple types of objects within a single print() function, then we have to use ‘+’ symbol between every object. And also we have to typecast every type object (other than string) to string type because if there is at least one string object in a print() function, the other objects must have to be of string type otherwise, we will get an error. As example:
    # Storing integer value in a, so a is of integer type
    
    >> a=95
    
    >>> type(a)
    <class 'int'>
    
    # This is the correct way to print multiple type of objects
    # using single print statement, we have typecasted a to string within the
    # print() function
    
    >>> print("Rajesh has scored "+str(a)+" marks out of 100")
    Rajesh has scored 95 marks out of 100
    
    # This is the wrong way to print multiple type of objects
    # using single print statement, we haven't typecasted a to string within the
    # print() function, so we are getting error
    
    >>> print("Rajesh has scored "+a+" marks out of 100")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: must be str, not int

     

  • We can do some mathematical calculations within print() function. We have to pass the operators and the operands in order inside print() function. As example:
    >>> print(2+3+5)
    10
    
    >>> print(2**3)      # ** is used to calculate power
    8
    
    >>> print(10/5)
    2.0
    
    >>> print(10//5)
    2

     


This was all about print() function.

Leave a Comment

Your email address will not be published. Required fields are marked *