The power of Python's print function
Briefly

The power of Python's print function
"Python's f-strings pair very nicely with the print function. But keep in mind that the print function also accepts multiple arguments. So if you're just putting spaces between a couple variables/strings: You could pass multiple arguments to print instead: Python's f-strings are great, but if you just need to smoosh a few values together with spaces between them, consider passing separate arguments to 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:"
"Why bother using * when you could just use the string join method? Well, it's a little bit shorter to write but it's also more flexible. 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 accepts any number of positional arguments and prints them separated by a default space. Passing separate arguments to print can be simpler than using f-strings or manual concatenation when only spaces are needed between values. The * operator can unpack iterables, including lists and generators, letting print output each element without explicit loops. Print automatically converts non-string objects to strings, avoiding the TypeError that occurs when joining non-string elements with str.join. The sep parameter lets callers customize the separator between printed values, providing flexibility beyond the default space.
Read at Pythonmorsels
Unable to calculate read time
[
|
]