Skip to main content

Attributes of print statement in python

Python print() statement

In our previous tutorial we have seen how to use print() statement.
Click here! if you haven't seen that tutorial.

The full syntax of print() statement is  

print("The statement to be printed",sep="The separator you want to insert between words",end="What you want to print at end of line", file=sys.stdout ,flush=False)

Here,

  •  * - Here * means that we can give more than one print statement separated by commas

  • sep -  Separators can be used when uou want to sepereate  Words by some specific character. Default value of separator is " "(single space) 

  •  end- It simply indicates that how do you want to end your line.Default value of end="\n"(leave a single line)

  •   file-- We use this attribute to write something into a file. That             text will not be displayed in the terminal.
     
  •  flush - decides whether to flush the stream or not. Default Value="False"
Note: In python you can use both " "(double quotes) or ' ' (single quotes) they both convey the same meaning.

Examples:

>>>  print("Where are you going?", "I am going to the shop")
  Output :  Where are you going? I am going to the shop


>>>print("Where are you going?", "I am going to the shop",sep="-")
 Output : Where are you going?-I am going to the shop


>>>print("10","7","1965", sep="/")
  Output :  10/7/1965

Notice that sep work on the values we assign to it. Default value of sep is " "(a single space).

You can simply go to your pycharm terminal and can run these commands or you can create a separate  python file and run it

eg.         print("Sun rises in east")
              print("and sets in west")

   Output :  Sun rises in east
                    and sets in west

         
eg.        print("Sun rises in east",end=" ")

             print("and sets in west")

    Output :  Sun rises in east and sets in west

eg.        print("Sun rises in east",end=" and")
             print(" sets in west")

    Output :  Sun rises in east and sets in west

Note: We will see "file" attribute of print() statement in upcoming tutorials after studying "FILE HANDLING"
                    


Comments

Popular posts from this blog

How to install python IDE ? IDLE and Pycharm

How to install Python IDE on Windows 10(Pycharm and IDLE)? Pycharm is the best Python IDE available.It comes with many in -built features. We will discuss its shortcuts in our next blogs.Pycharm is a cross-platform editor developed by jetbrains.   Installing Python Step 1) Visit the official website of python or click the below link              https://www.python.org/downloads/  .  Step 2) Click on Download Python               Step 3) After the download is complete ,open the exe file and click               on Install now  Step 4) After the successful installation it will show you a message         Setup was successful Step 5) Now go to your windows search bar and type "IDLE"              Press Enter and you will see that idle is opened Installing Pycharm St...

String Slicing in python

String Slicing Python  Slicing refers to extracting some words from a string. As in many other languages there are many different concepts for extracting a letters or words from a string ,in python we are having the concept of slicing. We can also the same things using looping, we will learn about it in our next tutorials. Indexing in Strings Before starting slicing, we need to understand the indexing in python. We have the concept of positive indexing  and  negative indexing.  Positive indexing starts from 0 to (length of string).It goes from forward to backward. Negative indexing starts from -1 to -(length of string + 1). It is illustrated in below example Below are some examples of how to use string slicing to extract characters from string. Accessing the characters in a string using positive index eg. string= "We are studying string slicing" s=string[ 0 ] # A single character will be printed present at index 0 w=string[ 4 ] # A...