Readings: [1.2]: Displaying Output in the Console

Introduction

As with all programming languages, coders use an editor to enter statements. Some editors also have the ability to run or execute the statements.

Using IDLE

For your first assignment, you will use the IDLE code editor that comes with Python. IDLE has a "Shell" that runs Python statements immediately. To start: find and open IDLE. The interface will look something like this:

Initial IDLE Screen
Initial IDLE Screen

By entering a valid Python statement and pressing [Enter], Python will "interpret" and execute the statement immediate. For example,

print("My name is David")

Simple Print Statement and Output
Simple Print Statement and Output

The print() statement is a built-in Python function that displays the output to the screen. All functions require parenthesis and print() accepts zero, one, or more values (arguments) inside the parenthesis to tell print() what to display.

It is permissible to use print() without any value(s), in which case print outputs only a carriage-return or next-line character and the display advances to another line. Using print("") or print(" ") (empty quotes or just spaces) will create the same effect.

The Python interpreter will ignore attempts to use print without parenthesis. Leaving out one quote mark or a parenthesis will cause the Python interpreter to "throw an exception". Try it.

IDLE Print Error Example
IDLE Print Error Example

EOL means "End of Line".

Literals and Variables

The value(s) provided to the print() function can be literals or variables. A literal is an actual value such as "Hello", "Sam Smith" 45 or 3.45. A variable is a storage container you define with a name, such as newGreeting, MyName, user_age or avg_Score. In Python, you declare a variable by assigning a value to it, such as

newGreeting = "Hello"
MyyName = "Sam Smith"
Notice that when assigning a variable a text value, such as "Hello", the value is surrounded by quotes. It is permissible to use either single quotes('') or double quotes("") for this purpose in Python. Numeric values, such as 45 or 3.45 do not have quotes. If you place a number in quotes, such as "45", that value is a string (text) - not a number.

More Ways to Use print()

The print() statement is very versatile. Multiple values can be supplied inside the parenthesis. Values supplied to a functions are called arguments. When using print, if multiple arguments are provided, separated by commas, print() will include all of them in a single display line. For example:

Example of print() Using Multiple Arguments
Example of print() Using Multiple Arguments

Notice that both strings and numbers can be used when providing multiple arguments to the print() function.

Another way to include multiple values when using the print() statement, is to concatenate the values into a single string. Concatenate means adding strings together to build a longer string. The is done by using the "+" operator. For example: Concatenating With a Variable

Concatenating With a Variable
The first example above concatenates two strings. The second example above concatenates a literal string ("David") with a variable string (myName).

One important rule about concatenation: numbers cannot be concatenated as they are. Numbers must be converted to strings. This is accomplished using Python's str() function, which converts a number into a string. Look at the examples below:

Concatenating With str()
Concatenating With the str() function

The first statement results in an error, because a number cannot be concatenated with a string. (Using the "+" operator with two numbers just adds them; it does not concatenate them.)

A Better Way to print() with Multiple Values

While both of the methods above can be used to use print() with one or more arguments, both of those methods are a little clumsy. There is a much better way: an f-string. An f-string is not a function, but a way to provide and format one or more arguments in the print() function. An f-string is created by prefixing a string argument with "f". Values can then be included in the string using curly brackets, such as the following examples:

F-String print() Examples
F-String print() Examples
Within the f-string, each arguments can be a literal or variable, and a string or a number. Each argument is enclosed in curly-brackets. F-strings are an example of string interpolation: creating strings with placeholders that are populated with literal or variable values, something available in most modern languages.

Keywords Covered in this Article