Thursday, December 16, 2021

SPT #5: The endless print

 The blog roars back to life; with a hiss and a clank, suddenly a new Stupid Python Trick appears!

This one's a quickie. Python`s print() function takes an end argument that lets you change what gets printed after the rest of things in that call have been printed. By default, this is a newline. It's common to set this to an empty string to suppress the newline:

print("There is no newline", end="")

But you can do the same by just including what you want to print as the end argument.

print(end="There is no newline")

By my count, that saves us four whole keystrokes: two quotes, a comma, and a space! Are we feeling stupid yet?