Where can I find a comprehensive list of ALL print() arguments?

Where is the official definition of all the functions built into Python, including all arguments and what they do?
Specifically, I would like to know all available options for the print() function using sys.stdout. There seems to be no official definition of the print function, and only a general description about its capabilities on doc.python.org.

https://docs.python.org/3/library/functions.html#print

4 Likes

Hello, @Sunday_scour, and welcome to Python Software Foundation Discourse! We hope you enjoy these discussions.

Note that at the top of the page to which @petersuter sent you there is a list of all the Python built-in functions.

Click on the name of any function to see its official documentation.

I’m curious what is missing from the documentation of print.

From my own notes, which have been complied from a number of different sources, I have this:

print()

Syntax

print(object(s), sep=separator, end=end, file=file, flush=flush)

Parameter Description
object(s) Any object (and as many as you like) will be converted to string before being printed
sep='separator' Optional: Specify how to separate the objects, if there is more than one. Default is a space
end='end' Optional: Specify what to print at the end. Default is '\n'
file Optional: An object with a write method. Default is sys.stdout
flush Optional: A Boolean, specifying if the output is flushed (True) or buffered (False). Default is False

I know a bit of some other languages, but am new to Python. It’s for my CSC 221 class at college. After going on and on about how simple, and natural, and easy to use Python is, like print(my_var, 20+70, “My name is”, name) etc., they throw us this monstrous bit of code to copy and paste without explanation:

print(f’{your_value1:.2f} {your_value2:.2f} {your_value3:.2f}')

The teacher is just like “you’ll have to use code you don’t understand all the time in your career, get used to it.” And I am thinking, this is a classroom where I’m supposed to learn coding and programming. An explanation for students is kind of required by the job description.

Anyway, after crawling around on the web I finally found out about the new f-strings capabilities. No where are they mentioned in the print function documentation. It was only by digging through the source code that I verified that f’{} must be a separate type of OBJECT being passed to the print function, NOT an argument for it. When I saw the term “f-strings” and figured out it was a new addition to Python, I was good. Why couldn’t zyBooks OR the professor just have said “Here is some more information on f-strings for those that want to know more than we demonstrated here,” with a link to a website on f-strings. I just don’t understand why they didn’t do that.

That’s because f-string (or formatted string literal) is not restricted to the print() function.

I can sense your frustration and understand it, but help is on hand, right here, as long as you’re prepared to put in the work, which it seem to me, that you are.

edit to add link:

1 Like

Note that f-strings aren’t anything to do specifically with the
print() function, it can print the representation of any data type.
Just like if you were to print(23) you wouldn’t expect the
documentation about the print() function to explain integers.

2 Likes

There’s two things going on there and it must seem to be confusing for you, or anyone, that does not know, as they both use f.

The first f is for the f-string. The :.2f is to format a floating point number to two decimal places.

I’m overwhelmed by y’all’s responses! Quite a lively community!

Rob → Thanks for the link! Once I figured out that I needed to look for f-string usage, I found plenty of examples, but I’m glad you found it in the OFFICIAL documentation. Definitely bookmarking that one. I got the .2f meaning right away, I just couldn’t figure out what the FIRST f in the print() argument was for.

fungi → Very cool! I’m glad that it’s NOT just for the print() function, but for any string data. I imagine that will come in handy for more advanced structured input output. I had tried to side-step the unknown code by using round(my_var, 2), but regardless of how many digits you specify, print() will always chop the trailing zeros, and my code submission failed the automated tests. F-strings are the best way to go, without involving into the format() function…

1 Like

The official Library Reference says “keep this under your pillow,”
so I do. Absorbed lots of Python that way! :wink:

Ha! :rofl: Of course, I like a cool pillow, and my laptop generates a bit too much heat. . .:wink:

Ha ha ha ha, of course they did. rolls eyes

The monster there is the f-string, which despite the name is not really a string, it is a sort of mini-programming language which generates a string. When f-strings were first suggested as an addition to Python, I knew it would make it harder for beginners.

And this attitude is shockingly bad, and probably means that the teacher doesn’t understand it either:

Okay, let’s break it down. print is a red-herring. The print function doesn’t know anything about f-strings, all it sees is the generated string.

So we need to look at the f-string: f'{your_value1:.2f} {your_value2:.2f} {your_value3:.2f}'.

The format of an f-string is that it looks like a string, except that parts inside curly brackets – braces {} – are treated as code plus formatting instructions.

So the section {your_value1:.2f} has code your_value1, which merely evaluates the variable of that name, and formatting code “.2f”, which expects a floating point number and displays it with two decimal places.

Likewise for the other two sets of curly brackets. And there are spaces between them, so you end up with something like “1.25 3.57 9.01”. And it is that string which print() gets handed to print.

So it’s not really so bad, after all, it just needs an explanation.

Presumably your teacher either doesn’t understand it, or belongs to the school of thought that second year students should be able to research things themselves and the only purpose of the teacher it to dump code in their lap and then walk away.

(Which kinda defeats the purpose of a teacher, right?)

1 Like

Yeah, I think it would have been easier to digest if the output string had been assigned to a separate variable then that variable passed to print(), which is a function we’re currently learning about. Having it all on one line made me think that those were all parameters of the print function itself. Thus my search for all the arguments for print().

Why not just use help(print)?

Help on built-in function print in module builtins:

print(*args, sep=' ', end='\n', file=None, flush=False)
    Prints the values to a stream, or to sys.stdout by default.
    
    sep
      string inserted between values, default a space.
    end
      string appended after the last value, default a newline.
    file
      a file-like object (stream); defaults to the current sys.stdout.
    flush
      whether to forcibly flush the stream.

That’s a cool feature! Another thing I wish they’d have covered in the zyBooks course. I resorted to digging through the source code. I found the implementation of the print() function.

By Sunday Scour via Discussions on Python.org at 17Sep2022 09:45:

Yeah, I think it would have been easier to digest if the output string
had been assigned to a separate variable then that variable passed to
print(), which is a function we’re currently learning about. Having it
all on one line made me think that those were all parameters of the
print function itself. Thus my search for all the arguments for
print().

Yes, as a piece of tutorial code that’s confusing.

But on the upside, it shows something general about Python (and many
languages): all the parameters to a function are evaluated before the
function is called. And then the funct (eg print() gets called with
the values).

So:

 msg = 'foo'
 x = f'msg is the string {foo!r}'
 print(x)

and:

 msg = 'foo'
 print(f'msg is the string {foo!r}')

both call print() with the string:

 msg is the string 'foo'

and that applies to any function call. No function has any funny
special powers, and so there’s no magic suite of complex rules to learn
about parameters, as you had feared about print().

Cheers,
Cameron Simpson cs@cskk.id.au

Excellent info, thank you!

I first started with computers using MS-DOS 2.50 or something on a Tandy 1000EX. I remember trying to figure out, learn, and memorize all the “fancy” arguments and parameters that could be passed to all the various .exe and .com programs, and all the switches and things for the built-in commands. I just default into assuming there is some hidden code that I’m not getting, built into the function. Which, in this case, was partly correct. It was just a code in a different type of object, f-strings, and not the print function itself, which merely takes the f-string object like it would a literal string encased in quotes.

To make it just a BIT more confusing, I come from BASIC and Pascal and C++, and all my strings get DOUBLE quotes on each end. However the zyBooks course and teacher love to take full advantage of Python’s lax syntax rules to minimize the number of quotation marks used, and solely use single quotes for any kinda of string. And my Calculus experience makes me look at f’ and see not and f with an open single-quote, but a derivative of the f function: f’ (f prime). And don’t get me started on using ** instead of ^ to indicate powers. . .

Anyway, it all makes sense now.

1 Like

I’m sorry. I didn’t mean to ignore you; I simply missed your comment/question.

In my notes, I have links to related items. For example, list has links to and from other data types, print() has links to and from f-string and so on.