The power of Python's print function
Briefly

The power of Python's print function
"Python's print function is usually one of the first built-in functions that new Pythonistas learn about, but many Python programmers don't realize how versatile the print function is. Let's look at some of the secret powers of Python's print function. Using multiple arguments Python's print function can accept more than one argument: Python's f-strings pair very nicely with the print function."
"Unpacking an iterable into print Python's print function doesn't just accept more than one argument, it accepts any number of positional arguments. So if we wanted to print every item in a list, we could use the * operator to pass that list to print: This doesn't just work on lists. It works for any iterable. For example we could unpack a generator object into print to see each value in it:"
"Let's say we'd like to print numbers with spaces between each one. If we use the join method we'll get an error when joining numbers together: We need to convert each item to a string to join them. We could use a list comprehension or a generator expression for that: But the print function does this conversion automatically: The print function will automatically convert whatever object you give it to a string because it needs to be a string in order to be printed."
Python's print function accepts any number of positional arguments and separates them by spaces by default. The print function can receive multiple arguments instead of concatenating strings with f-strings or manual spacing. The * operator unpacks iterables so that each element can be printed as a separate argument. Print automatically converts non-string objects to strings, avoiding the need to map or join elements before printing. The print function can be customized using the sep parameter to change the separator between values. These features make print a concise, flexible tool for formatting, quick output, and debugging.
Read at Pythonmorsels
Unable to calculate read time
[
|
]