2.3.3 Text Output

Trinket is a great tool for drawing shapes and pictures, but sometimes we might want to display less flashy but still useful textual output as well. There are two ways we can do this, and we’ve already briefly seen the print command at the start of this chapter. As a reminder, it looks like this:

The print command displays the text that you provide between the parentheses to the console, which is the grey area to the right of your coding window. Notice that double quotation marks enclose the text we want to display (“ and ”), and those quotes do not display along with the text when the print command executes. In Python, any time you’re working with words or sentences that you want to be displayed “as is,” you need to enclose them in double-quotes. For instance, this print statement will result in an error.

As we’ve seen with the other commands in this chapter, you don’t need the quotation marks when dealing with numbers. You only need them with text. In later chapters, we’ll say more about why this is so. Trinket in Processing Mode also lets you display text on its canvas. For that, we use the text command, like so:

You’ll notice that the text command needs two additional numbers that print did not. That’s because since we’re using the canvas now, the text command needs to know not only the text that you want to display, but also where on the canvas to put it. The two numbers give the (x, y) coordinates of where to place the text.