Tutorial

Python print()

Published on August 3, 2022
author

Pankaj

Python print()

Hello learners. In this tutorial we are going to learn more about Python print function. In our last tutorial we learned about Python float function.

Python Print

Almost all of our previous tutorial contains the Python print() function. But we did not discussed about python print function to the fullest. Now we will learn it. At first we should know about the basic structure of python print function. That is given below; Python Print Format If you read our tutorial on Python Functions and arguments, then you should probably had the idea about the above function. The values receives a list of undefined variables. So, all the comma separated values will goes under the list values. So if you add more elements separated by comma, you will get a output where all the values are put together separated by space. The following example will guide you about the simple python print function usage.

# initialize 1st variable
var1 = 1

# initialize 2nd variable
var2 = 'string-2'

# initialize 3rd variable
var3 = float(23.42)

print(var1, var2, var3)

The output of the following code will be.

1 string-2 23.42

So, as many item you want to print, just put them together as arguments.

Using sep Keyword in python print function

If see the example of the previous section, you will notice that that variables are separated with a space. But you can customize it to your own style. Suppose in the previous code, you want to separate the values using underscore(_). Then you should pass underscore as the value of sep keyword. The following function will illustrate you the idea of using python print sep keyword.

# initiaze 1st variable
var1 = 1

# initialize 2nd variable
var2 = 'string-2'

# initialize 3rd variable
var3 = float(23.42)

print(var1, var2, var3, sep='_')

And you will get your desired output like this.

1_string-2_23.42

Python print end keyword

The end key of print function will set the string that needs to be appended when printing is done. By default the end key is set by newline character. So after finishing printing all the variables, a newline character is appended. Hence, we get the output of each print statement in different line. But we will now overwrite the newline character by a hyphen(-) at the end of the print statement. See the following example.

# initialize a list
initList = ['camel', 'case', 'stop']

# print each words using loop
print('Printing using default print function')
for item in initList:
    print(item)  # default print function. newline is appended after each item.

print()  # another newline

# print each words using modified print function
print('Printing using modified print function')
for item in initList:
    print(item, end='-')

And you will get outputs like the following

Printing using default print function
camel
case
stop

Printing using modified print function
camel-case-stop-

Python print to file

In this section we will learn about file keyword. Actually file keyword is used for extracting output to a specified file. If you read our previous tutorial Python file operation, then you should know about basic file operation. So, you have to open a file in writable mode first, then use the file pointer as the value of file keyword in print() function. See the following code to understand the python print file usage.

# open a file in writable mood
fi = open('output.txt', 'w')

# initialize a list
initList = ['camel', 'case', 'stop']

# print each words using loop
print('Printing using default print function')
for item in initList:
    print(item, file=fi)  # use file keyword

print(file=fi)

# print each words using modified print function
print('Printing using modified print function')
for item in initList:
    print(item, end='-', file=fi)  # use file keyword

# close the file
fi.close()

And you will get the same output as the previous example in an output text file. That’s all about Python print. Hope that you understood it well. For any further query, feel free to use the comment section. Best of luck.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
April 17, 2020

What does flush do? Thanks a lot for your mega cool article!

- Emy

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    April 25, 2019

    Great content useful for all the candidates Python training who want to kick start these career in Automation Python training field.

    - python Training in Hyderabad

      Try DigitalOcean for free

      Click below to sign up and get $200 of credit to try our products over 60 days!

      Sign up

      Join the Tech Talk
      Success! Thank you! Please check your email for further details.

      Please complete your information!

      Featured on Community

      Get our biweekly newsletter

      Sign up for Infrastructure as a Newsletter.

      Hollie's Hub for Good

      Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

      Become a contributor

      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

      Welcome to the developer cloud

      DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

      Learn more