DEV Community

loading...

Python’s Print() Does What!?!?

Alamar
Mostly faithful Pythonista, though I dabble in Haskell. Interested in and employed for data engineering
・2 min read

The Basics

Note: The following code is valid only for Python 3.0 and up.

If you’ve gone through even one tutorial of any programming language, you know the first thing you do is print “Hello World” to the console. Python’s way of doing that is, of course, print(). And it’s as simple as typing the line

print("Hello Word")
# Hello World

Tutorial over, right? Well, no. Turns out print() has some hidden use cases.

Print()+

The print function takes multiple objects so you can do some goofy things like:

print("my", "name", "is", "Austin")
# my name is Austin

This returns my name is Austin because print’s default separator is a space(‘ ‘). This can easily be changed though by adding the keyword argument sep after the things to print. In the example of:

print("my", "name", "is", "Austin", sep="")
# mynameisAustin

the output mynameisAustin isn't pretty, but it is functionality that could come in handy.

There is also the option of using the keyword argument end, which defaults to a line break(“\n”).

print("my", "name", "is", "Austin", sep=",", end="!") 
# returns: my,name,is,Austin!
# you can actually use any valid string in the keyword argument end

Now we’re done, right? Nope. There’s more goodness packed into this function.

Print()++

What if you wanted a log of everything you printed? Well, print() already has that functionality. All you need to do is use the keyword argument file. Let’s say I wanted a .txt document as the output of the last print statement:

with open("text.txt", "w") as text:
    print("my", "name", "is", "Austin", sep=",", end="!", file=text)
# this take the output as before (my,name,is,Austin!) and outputs it
# into text.txt

(More to come on the open() function, it's got some neat things to explore as well)

This is powerful alternative to printing everything to the console and can help log potential errors while debugging.

Like many things in programming, print() is something we often take for granted without really looking at it too deeply. Now that you’ve seen what print can actually do, I hope that it will help you in your development.

Thanks for reading!

Discussion (10)

Collapse
jessekphillips profile image
Jesse Phillips

Nicely written up. I'm still not convinced Python makes for a clear and readable language

I take posts like this and compare it to D. The last one on list comp was fun too.

Collapse
safijari profile image
Jariullah Safi

Please stop posting these. They're not helpful to the people reading these articles and would serve to defeat your propose of promoting D by creating an association between D and these annoying first post on python articles.

If you really want D to succeed, simply evangelize it. Don't use it as a way to attack others.

Collapse
jessekphillips profile image
Jesse Phillips

I've chosen to reply with an article. This will make it more reusable.

Collapse
byrro profile image
Renato Byrro

The print statement is definitely not the reason why python would be considered a readable language

Collapse
jessekphillips profile image
Jesse Phillips

Not saying it was. Take a look at my list comprehension comparison. Again probably not why Python is considered readable (but still a touted powerful tool Python provides).

If you send me some readable Python, if it isn't too massive I could try and use it to make a compare.

Thread Thread
byrro profile image
Renato Byrro

Wasn't familiar with this Dlang until I crossed your comment.

If you think Python list comprehension is less readable we have very different personal preferences in terms of reading code...

Even basic functions. I mean, what does iota even mean? It's obvious what range means, even for someone that has never came across Python before.

Thread Thread
jessekphillips profile image
Jesse Phillips

dev.to/jessekphillips/comment/j0pa

Moving the discussion since my article is about comparison and this one is not.

Collapse
lietux profile image
Janne "Lietu" Enberg

Some of this stuff depends on features that aren't in older Python versions out of the box and you might need to do this if you're e.g. building a library that you want to support older Python versions.

from __future__ import print_function

Honestly I've almost never found any practical use for these things, format strings are much better for actually building output.

I think I've tried to use file=sys.stderr a few times.

Collapse
alamarw profile image
Alamar Author

Yeah, you’ve got a good point there. I’ll edit the post so that it says which versions of python this sort of functionality is compatible with. Thanks for the tip!

Collapse
alamarw profile image
Alamar Author

Change the post to reflect that it only works on Python 3.0 and up. Thanks again for the heads up!