Jump to content

Python Print Function


Recommended Posts

Python is one of the modern, multi-purpose, and high-level programming languages. Python is used for various purposes i.e. software development, data science, machine learning, artificial intelligence, deep learning, and back end development. More often, in any programming language, we need to print some data or string on the console. In python, we use the print () function to print the string or any kind of data on the console.

In this article, we will see how we can use the print () function in python for printing purposes.

Let’s begin this article by printing the hello world in python.

To print the Hello World in python 3, make use of the print () function as follows:

word-image-1654.png

And the output is

word-image-1655.png

Unlike in python 2, we do not use parenthesis with the print function. In python 2 it would be like this

word-image-1656.png

And the output is

word-image-1657.png

Furthermore, in this article, we will follow the python 3 syntax.

Let’s print the “Welcome to LinuxHint” by using the print function.

print(“Welcome to LinuxHint”)

Output

word-image-1658.png

Print name of the Animals

If you want to print the name of the animals, you can print in the following way:

print ("Cow")
print("Dog")
print("Cat")
print("Lion")

Output

word-image-1659.png

This is the basic syntax of the print () function. We can use multiple parameters with the print function. The following are the parameters of the print () function:

  • Objects: Objects indicate the objects that are to be printed in the print () function.
  • Sep: It is used to separate the objects in the print function. We can use ‘,’ as a sep. the default value of sep is ‘’.
  • End: In python, the print function end by default with a new line ‘\n’. You can use any value to end the python print function.

Print multiple objects in one print statement

Consider the animal example that we used previously to print the name of animals. Previously we used multiple print statements to print the name of animals. In this example, we print the name of various animals in a single print statement. The animals are objects. The objects are separated by a sep which is ‘,’.

print("Cow","Dog","Cat","Lion", sep =',')

In the above line of code cow, dog, cat, and lion are the objects, and ‘,’ is a separator.

Output

word-image-1660.png

Print statement with end parameter

As we know, the print statement end with a new line by default but we can use any value to end the python print statement. We can end a line with any string or character. Python 2 does not support it.

For example, the print statement ends with ‘!’.

print ("Hello Everyone", end = ' ')
print ("Welcome to the LinuxHint", end = '!')

Output

word-image-1661.png

The print statement end with ‘@’

print ("An email must contain", end = '@')

Output

word-image-1662.png

Print blank lines

Python allows us to print the blank lines in the print () function. Sometimes we need to print the blank lines. To print the blank lines we use ‘/n’ in python.

Example

Print 6 blank lines. You can write it as follows:

print (“\n\n\n\n\n\n”)

or you can also write it as

print (6* “\n”)

Code example

print ("Hello Everyone")
print (6*"\n")
print ("Welcome to the LinuxHint")

Output

word-image-1663.png

Conclusion

The print () function is used for printing the strings, objects, characters, and it is also used for debugging purposes. In this article, we have explained the python print () function and its usage with multiple examples.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...