/  Technology   /  Different Styles Of Print Statements In Python Programming
Different Styles Of Print Statements In Python Programming

Different Styles Of Print Statements In Python Programming

What is a Print Statement?

Print statement is used to send the output to the computer screen. The message can be string or any other objects.

Syntax

print (objects, sep = ‘seperator’, end = ‘end’, file = file, flush = flush)

 

Arguments                                              Description

objectsAny objects as many as you like
seperatorHow to separate objects (Optional)
endWhat to print at the end (Optional)
fileAn object with a write method (Optional)
flushA Boolean value , By default it is False (Optional)

 

HOW TO PRINT A STRING?

To print the string, we have to use the print () Function and inside the double quotes (“”) just enter the string you want to print

 

PROGRAM

Different Styles Of Print Statements In Python Programming

OUTPUT

Different Styles Of Print Statements In Python Programming

We can also perform arithmetic operations inside the print () function. Let’s see an example of addition of two numbers.

  

PROGRAM

Different Styles Of Print Statements In Python Programming

OUTPUT

30

 

HOW TO USE SEPERATOR METHOD?

To use the seperator method we just have to add a parameter to the print () Function which is sep

 

1) Without using sep method

PROGRAM

Different Styles Of Print Statements In Python Programming

OUTPUT

Different Styles Of Print Statements In Python Programming

As you can see the strings are separated by space(default). Now by using sep method we can specify how the strings should be separated from each other.

 

2) Using sep method

PROGRAM

 Different Styles Of Print Statements In Python Programming

OUTPUT

Python,Programming,Language

As you can see now the strings are separated by comma (,). Similarly, you can decide how you want to separate the strings.

 

HOW TO USE END METHOD?

To use the end method, we have to pass a parameter to the print () function which is end.

1)Without using end

Different Styles Of Print Statements In Python Programming

OUTPUT

10
20

As you can see the output is in the different line but if you want that the end of this line should be with comma (,) you can do that with the help of end method let’s see an example

 

2) Using end method

 Different Styles Of Print Statements In Python Programming

 OUTPUT

10,20

 

HOW TO USE FORMAT METHOD?

The format () method formats the specified values inside the string’s placeholder. It returns the formatted string. Placeholder is defined by using curly brackets: {}.

Syntax

String.format(value1, value2)

The Placeholders can be identified using named indexes {object name}, numbered indexes {0} or even empty placeholder { }

 

Let’s see an example how this format () methods works.

1)Numbered Indexes

PROGRAM

OUTPUT

The two numbers are 10 and 20

 

2)Named Indexes

PROGRAM

 Different Styles Of Print Statements In Python Programming

OUTPUT

Different Styles Of Print Statements In Python Programming

 

3)EMPTY PLACEHOLDER

PROGRAM

Different Styles Of Print Statements In Python Programming

OUTPUT

Different Styles Of Print Statements In Python Programming

There is another way to print statement in python whose syntax is similar to C programming syntax

PROGRAM

Different Styles Of Print Statements In Python Programming

OUTPUT

Different Styles Of Print Statements In Python Programming

In this way we have discussed the different styles of print statement in python programming.

Leave a comment