[ Web Proxy ]
URL:
Viewing: https://www.python-course.eu/python-tutorial/output-with-print.php [Back]  [Original]

21. Output with Print | Python Tutorial | python-course.eu
python-course.eu
Search site:

Live Python classes by highly experienced instructors:

instructor-led training course [instructor-led training course]
Instructor-led training courses by Bernd Klein

  1. Intro to Python Tutorial
  2. History and Philosophy of Python
  3. The Interpreter, an Interactive Shell
  4. Execute a Script
  5. Structuring with Indentation
  6. Assignment Expressions
  7. Data Types and Variables
  8. Type Annotations
  9. Operators
  10. Sequential Data Types
  11. List Manipulation
  12. Shallow and Deep Copy
  13. Dictionaries
  14. Sets and Frozen Sets
  15. Sets Examples
  16. Keyboard Input
  17. Conditional Statements
  18. Structural Pattern Matching
  19. Adventure Game with Structural Pattern Matching
  20. While Loops
  21. For Loops
  22. Output with Print
  23. Formatted Output
  24. Working with Dictionaries and while Loops
  25. Functions
  26. Passing Arguments
  27. Parameters And Arguments
  28. Namespaces
  29. Global vs. Local Variables and Namespaces
  30. File Management
  31. Modular Programming and Modules
  32. Packages
  33. Errors and Exception Handling

letterbox [letterbox]

This website contains a free and extensive online tutorial by Bernd Klein, using material from his classroom Python training courses.

If you are interested in an instructor-led classroom training course, have a look at these Python classes:

instructor-led training course [instructor-led training course]
Instructor-led training course by Bernd Klein at Bodenseo

Image kabliczech - Fotolia.com

This page was written by Bernd Klein.

Bernd is an experienced computer scientist with a history of working in the education management industry and is skilled in Python, Perl, Computer Science, and C++. He has a Dipl.-Informatiker / Master Degree focused in Computer Science from Saarland University.

PDF versions of this site [PDF versions of this site]

PDF logo [PDF logo] PDF version of this site

This website is free of annoying ads. We want to keep it like this. You can help with your donation:

[Go]

The need for donations

21. Output with Print

By Bernd Klein. Last modified: 08 Nov 2023.

Introduction

In principle, every computer program has to communicate with the environment or the "outside world". For this purpose nearly every programming language has special I/O functionalities, i.e. input/output. This ensures the interaction or communication with the other components e.g. a database or a user. Input often comes - as we have already seen - from the keyboard and the corresponding Python command, or better, the corresponding Python function for reading from the standard input is input().

We have also seen in previous examples of our tutorial that we can write into the standard output by using print. In this chapter of our tutorial we want to have a detailed look at the print function. As some might have skipped over it, we want to emphasize that we wrote "print function" and not "print statement". You can easily find out how crucial this difference is, if you take an arbitrary Python program written in version 2.x and if you try to let it run with a Python3 interpreter. In most cases you will receive error messages. One of the most frequently occurring errors will be related to print, because most programs contain prints. We can generate the most typical error in the interactive Python shell:

print 42
  File "", line 1
    print 42

OUTPUT:

  File "<ipython-input-1-5d54469f7ddf>", line 1
    print 42
           ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(42)?

This is a familiar error message for most of us: We have forgotten the parentheses. "print" is - as we have already mentioned - a function in version 3.x. Like any other function print expects its arguments to be surrounded by parentheses. So parentheses are an easy remedy for this error:

print(42)

OUTPUT:

42

But this is not the only difference to the old print. The output behaviour has changed as well.

Letterbox as a symbol for the Communication between Computer program and user [Letterbox as a symbol for the Communication between Computer program and user]

Live Python training

instructor-led training course [instructor-led training course]

Enjoying this page? We offer live Python training courses covering the content of this site.

See our Python training courses

See our Machine Learning with Python training courses

The arguments of the print function are the following ones:

 print(value1, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

The print function can print an arbitrary number of values ("value1, value2, ..."), which are separated by commas. These values are separated by a space in the output. In the following example, we can see two print calls. We are printing two values in both cases, i.e. a string and a float number:

a =  3.564
print("a = ", a)

OUTPUT:

a =  3.564
print("a = \n", a)

OUTPUT:

a = 
 3.564

We can learn from the second print of the example that a blank between two values, i.e. "a = \textbackslash n" and "3.564", is always printed, even if the output is continued in the following line. This is different from Python 2, as there will be no blank printed, if a new line has been started. It's possible to redefine the seperator between values by assigning an arbitrary string to the keyword parameter "sep", i.e. an empty string or a smiley:

print("a","b")

OUTPUT:

a b
print("a","b",sep="")

OUTPUT:

ab
print(192,168,178,42,sep=".")

OUTPUT:

192.168.178.42
print("a","b",sep=":-)")

OUTPUT:

a:-)b

A print call is ended by a newline, as we can see in the following usage:

for i in range(4):
    print(i)

OUTPUT:

0
1
2
3

To change this behaviour, we can assign an arbitrary string to the keyword parameter "end". This string will be used for ending the output of the values of a print call:

for i in range(4):
    print(i, end=" ") 

OUTPUT:

0 1 2 3 
for i in range(4):
     print(i, end=" :-) ") 

OUTPUT:

0 :-) 1 :-) 2 :-) 3 :-) 

The output of the print function is send to the standard output stream (sys.stdout) by default. By redefining the keyword parameter "file" we can send the output into a different stream e.g. sys.stderr or a file:

fh = open("data.txt","w")
print("42 is the answer, but what is the question?", file=fh)
fh.close()

We can see that we don't get any output in the interactive shell. The output is sent to the file "data.txt". It's also possible to redirect the output to the standard error channel this way:

import sys
# output into sys.stderr:

print("Error: 42", file=sys.stderr)

OUTPUT:

Error: 42

Live Python training

instructor-led training course [instructor-led training course]

Enjoying this page? We offer live Python training courses covering the content of this site.

Upcoming online Courses

See our Python training courses

See our Machine Learning with Python training courses

top


Web Proxy Viewer  |  New URL  |  Original Page