Printing and print() function in Python

Printing the output of a program is the first thing we learn while learning a new language. It's because it is one of the most used functionalities in any programming language, and Python is no exception.

The most commonly used function to print data is 'print()' which is simple and straightforward. This function provides a lot of options to format the output data according to our requirements. In this post, Let's have a look at all of them in detail.


1. print() function:

The 'print()' function is the basic printing function available in Python. Though being Simple, it can do almost everything to tailor the output the way a common user wants to do .

To print a statement, just pass the statement within quotes inside the brackets of the print() function.

print('Hello World!')
 Output: Hello World!


We can print data by passing variables into the print function, just like we did previously. One exception is that we don't have to pass the variable within quotes.

 

a=5
print(a)
 Output: 5


We directly passed the variable into the print function without embracing it within quotes. For variables, we don't have to use quotes, while for strings(as mentioned in the first example), we have to use quotes. If you notice, while printing variables, the value associated with the variable will be printed.

By default, Python adds a newline character at the end of each printed value. This ensures that successive calls of the print function do not produce the output on the same line. 


print('Lowercase')
print('Python')
 Output: Lowercase
         Python


Here, the first print statement adds a newline character at the end of the output. So the actual output of the first statement will be 'Lowercase\n' and for the second statement, it is 'Python\n'.

Sometimes, we don't need a newline at the end of each statement. In other words, we might want to print two different statements in the same line. In such cases, there is an argument for our rescue.

The 'end' argument:

We can add an optional 'end' argument in the print function to modify the trailing newline character. By default, when not specified, the value of the argument is '\n'. But we can modify it by explicitly specifying in the print statement.

print('Lowercase',end='')
print('Python')
 Output: LowercasePython

Here, we have specified an empty string as the 'end' argument. Thus nothing is separating the first and second print calls, which gives us the above output in a single line since there is no newline character.

We are not limited to giving just empty string to the 'end' argument but any character or string we want.

print('This is the First Sentence ',end='. ')
print('This is the Second Sentence')
 Output: This is the First Sentence. This is the Second Sentence

The 'sep' argument:

The sep argument is used to define the separator which separates two or more different data given inside a print() function.

print('Welcome','to','Lowercase','Python')
 Output: Welcome to Lowercase Python

Here, we have given four Strings(words) to the print() function separated by commas. On the Output, we can see that instead of printing continuously, each String is separated by a space. This is because by default, the print function separates two different data given inside it by spaces. But we can modify it via the 'sep' argument.

print('Animals','Mammals','Humans','You',sep='->')
 Output: Animals->Mammals->Humans->You

Any string including an empty string can be used as an argument for 'sep' argument.

Comments

Popular posts from this blog

Time Complexity of Basic functions in Python

Variables in Python

Lists in Python